引言
尝试理解下面几句话
- type的父类是object
- object的元类是type
- 实例由类创建,类由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
解释:
- Father和Son的元类是Human
- Human的父类是type
- 在创建Father和Son两个类时,会调用Human中的init与new方法
- 在实例化Father和Son时,会调用Human中的call方法,call的第一个参数是自动传入的类的类型,然后根据类型去调用相应的init与new