测试代码如下:
复制代码 代码如下:
class ModelMetaClass(type):
def new(cls,name,base,attrs):
logging.info("cls is:"+str(cls))
logging.info("name is:"+str(name))
logging.info("base is:"+str(base))
logging.info("attrs is:"+str(attrs))
return type.new(cls,name,base,attrs)
pass
pass
class Model(dict):
metaclass = ModelMetaClass
def init(self):
pass
pass
def main():
m=Model()
if name == 'main':
main()
测试结果:
复制代码 代码如下:
INFO:root:cls is:<class 'main.ModelMetaClass'>
INFO:root:name is:Model
INFO:root:base is:(<type 'dict'>,)
INFO:root:attrs is:{'module': 'main', 'metaclass': <class 'main.ModelMetaClass'>, 'init': <function init at 0x025C76F0>}
结论就显而易见了。cls是当前类的名字,即类本身。name是通过metaclass属性指向ModelMetaClass的类,即要实例化的类,ModelMetaClass拦截了该类的实例化。base是要实例化的类的基类。attrs是要实例化的类的属性集合。
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8