Take a look at the following example
class Base(type):
def __init__(cls, name, bases, dict_):
print 'Base.__init__() with cls %s, name %s...' % (cls, name, )
type.__init__(cls, name, bases, dict_)
M = Base('M', (object,), {})
class D1(Base):
pass
class D2(M):
pass
The output is
Base.__init__() with cls <class '__main__.M'>, name M...
Base.__init__() with cls <class '__main__.D2'>, name D2...
I feel so puzzled about the result,
- Why
Base.__init__
be invoked forD2
, even we have not creat an instance ofD2
? - Since
Base.__init__
be invoked forD2
, whyD1
not?