元类碎屑
zx
class zx:
pass
xx=zx()
print(type(zx))
print(type(xx))
<class 'type'>
<class 'main.zx'>
由打印结果,可知,zx类的模板类为type,xx的模板类为zx
class zy(type):
def __init__(self):
pass
class zx(metaclass=zy):
def __init__(self):
pass
zx()
当运行到class zx这句时,就相当于与准备实例化一个类对象,和普通对象的实例一样,相当于
zy("zx",(object,),{属性字典})
运行会报错!
TypeError: __init__() takes 1 positional argument but 4 were given
__init__()
需要4个参数,我们就给了一个参数,由此可见,这个和真正的实例化一样,也会执行模板类的__init__()
方法
class zy(type):
def __init__(self,a,b,c):
pass
class zx(metaclass=zy):
def __init__(self):
pass
a=zx()
print(a)
<main.zx object at 0x000001BDA04D0400>
这样就可以正常运行了,a实对象也生成了
但是把代码改成这样
class zy(type):
def __init__(self,a,b,c):
pass
def __call__(self):
pass
class zx(metaclass=zy):
def __init__(self):
pass
a=zx()
print(a)
None
可知,类对象加括号,执行的,a为空了,说明了返回对象的其实是__call__()
方法
class zx(type):
def __init__(self,a,b,c):
print(1)
pass
def __call__(self, *args, **kwargs):
print(2)
obj=object.__new__(self)
self.__init__(obj,*args, **kwargs)
return obj
class zy(metaclass=zx):
def __init__(self,name,age):
self.name=name
self.age=age
a=zy("nick",18)
print(a)
print(a.__dict__)