class Meta(type):
def __call__(cls, *args, **kwargs):
print 'Meta.__call__ called.'
return super(Meta, cls).__call__(*args, **kwargs)
class MetaInstance(object):
__metaclass__ = Meta
# Instantiate class.
MetaInstance() # Meta.__call__ called.
class StandardClass(object):
@classmethod
def __call__(cls, *args, **kwargs):
print 'StandardClass.__call__ called.'
return super(StandardClass, cls).__call__(*args, **kwargs)
# Instantiate class.
StandardClass() # StandardClass.__call__ is not called!
Why metaclass's __call__
method called on class, but native class's __call__
not, when i instantiate a class?
When you create a class instance, the metaclass's (whose instance the class is)
__call__
is called.When you create a class instance and then "call" the instance, then the class's
__call__
is called. I don't think decorating__call__
withclassmethod
will work though.This will call your metaclass's and class's
__call__
s: