python中的元组metaclass

python中的元组metaclass

引言

尝试理解下面几句话

  1. type的父类是object
  2. object的元类是type
  3. 实例由创建,type创建

我们知道,python中一切皆对象,类(class) 也不例外。类(class) 作为一个对象,它需要有类型(type) ,像 [1,2,3] 这样的对象,其类型为list,而list的类型为type,而type自身的类型即为type,是递归定义的。
下面用代码来验证:

a = [1,2,3]
print(type(a))
print(type(type(a)))
print(type(type))

#output
#<class 'list'>
#<class 'type'>
#<class 'type'>

简单例子

class Human(type):
    def __new__(cls,name,father,contents):
        print("method:Human.__new__")
        print(f'address:{cls}')
        print(f'name:{name}')
        print(f'father:{father}')
        print(f'contents:{contents}')
        return super().__new__(cls,name,father,contents)
    
    def __init__(self,name,father,contents):
        print("method:Human.__init__")
        print(f'address:{self}')
        print(f'name:{name}')
        print(f'father:{father}')
        print(f'contents:{contents}')
        super().__init__(name,father,contents)
        print("initial down")
    
    def __call__(self,*args):
        print("method:Human.__call__")
        print(self)
        print(args)
        obj = self.__new__(self,*args)
        self.__init__(self,*args)
        return obj

class Father(metaclass=Human):
    def __init__(self,name):
        print(f'{self}.init')
        self.name = name

    def __new__(self,*args):
        print(f"{self}.new")
        return super().__new__(self)

    def dec(self):
        print("I'm father!")
    
class Son(Father):
    def __init__(self,name,age):
        print(f'{self}.init')
        self.name = name
        self.age = age

a = Father('Tom')
b = Son('Jerry',2)
c = Father("kl")

输出:

method:Human.__new__
address:<class '__main__.Human'>
name:Father
father:()
contents:{'__module__': '__main__', '__qualname__': 'Father', '__init__': <function Father.__init__ at 0x00000243132D5CA0>, '__new__': <function Father.__new__ at 0x00000243132D59D0>, 'dec': <function Father.dec at 0x000002431145B310>, '__classcell__': <cell at 0x0000024312F68B50: empty>}
method:Human.__init__
address:<class '__main__.Father'>
name:Father
father:()
contents:{'__module__': '__main__', '__qualname__': 'Father', '__init__': <function Father.__init__ at 0x00000243132D5CA0>, '__new__': <function Father.__new__ at 0x00000243132D59D0>, 'dec': <function Father.dec at 0x000002431145B310>, '__classcell__': <cell at 0x0000024312F68B50: Human object at 0x000002430F7BFB60>}
initial down
method:Human.__new__
address:<class '__main__.Human'>
name:Son
father:(<class '__main__.Father'>,)
contents:{'__module__': '__main__', '__qualname__': 'Son', '__init__': <function Son.__init__ at 0x0000024313069280>}
method:Human.__init__
address:<class '__main__.Son'>
name:Son
father:(<class '__main__.Father'>,)
contents:{'__module__': '__main__', '__qualname__': 'Son', '__init__': <function Son.__init__ at 0x0000024313069280>}
initial down
method:Human.__call__
<class '__main__.Father'>
('Tom',)
<class '__main__.Father'>.new
<class '__main__.Father'>.init
method:Human.__call__
<class '__main__.Son'>
('Jerry', 2)
<class '__main__.Son'>.new
<class '__main__.Son'>.init
method:Human.__call__
<class '__main__.Father'>
('kl',)
<class '__main__.Father'>.new
<class '__main__.Father'>.init

解释:

  1. FatherSon的元类是Human
  2. Human的父类是type
  3. 在创建FatherSon两个类时,会调用Human中的init与new方法
  4. 在实例化FatherSon时,会调用Human中的call方法,call的第一个参数是自动传入的类的类型,然后根据类型去调用相应的init与new
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值