概述
class Meta(type): def __call__(cls,*args): print "Meta: __call__ with",args class ProductClass(object): __Metaclass__ = Meta def __init__(self,*args): print "ProductClass: __init__ with",args p = ProductClass(1)
输出如下:
Meta: __call__ with (1,)
题:
为什么ProductClass .__ init__没有触发…只是因为Meta .__ call__?
更新:
class ProductClass(object): __Metaclass__ = Meta def __new__(cls,*args): print "ProductClass: __new__ with",args return super(ProductClass,cls).__new__(cls,*args) def __init__(self,args p = ProductClass(1)
调用ProductClass的__new__和__init__是Meta .__ call __的责任吗?
class Meta(type): def __call__(cls,args return super(Meta,cls).__call__(*args)
总结
以上是编程之家为你收集整理的python – 元类的“__call__”和实例的“__init__”的关系?全部内容,希望文章能够帮你解决python – 元类的“__call__”和实例的“__init__”的关系?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧