Initialization of base classes
Mark McEahern
marklists at mceahern.com
Mon Mar 18 17:07:14 EST 2002
[sorry.antispam at address.net]
> On pp 74 of Bealey's excellent book "Python Essential Reference, 2nd
> Ed" it is written:
>
> "When an instance is created [of a derived class], the __init__()
> methods of base classes are not invoked. Thus, it's up to a derived
> class to perform the proper initialization of its base classes, if
> necessary"
>
> Is this really true?
Consider this, though:
>>> class A:
... def __init__(self):
... print "A"
...
>>> class B:
... def __init__(self):
... print "B"
...
>>> class C(A):
... def __init__(self):
... pass
...
>>> class D(A, B):
... def __init__(self):
... pass
...
>>> w = A()
A
>>> x = B()
B
>>> y = C()
>>> z = D()
This seems to be the scenario the book is describing: If the derived class
defines __init__(), the base classes' __init__()s will not get called unless
you call them explicitly.
Cheers,
// mark
More information about the Python-list
mailing list