Say I've got a metaclass and a class using it:
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) Output as follows:
Meta: __call__ with (1,) Question:
Why isn't ProductClass.__init__ triggered...just because of Meta.__call__?
UPDATE:
Now, I add __new__ for ProductClass:
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): print "ProductClass: __init__ with", args p = ProductClass(1) Is it Meta.__call__'s responsibility to call ProductClass's __new__ and __init__?
Meta.__call__()doesn't return anything. It needs to return an instance of the class it's passed as it first argumentcls. This is usually accomplished by calling a method of the same name in its parent (aka base) class. That can be done by hardcoding it, i.ereturn type.__call__(, *args), or by usingreturn super(Meta, cls).__call__(*args).