Initialization of base classes

Joshua Marshall jmarshal at mathworks.com
Mon Mar 18 16:50:08 EST 2002


I believe it's the case that __init__ is just called on the object created.
So standard method-lookup rules apply.

sorry.antispam at address.net wrote:
> 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? If you have:

> class A:
>    def __init__(self):
>       print "Class A"

> class B:
>    def __init__(self):
>       print "Class B"

> class C(A):
>    pass

> class D(A, B):
>    pass

> Then:
> w = A() --> "Class A"
> x = B() --> "Class B"
> y = C() --> "Class A"
> z = D() --> "Class A"

> Hence, instantiating class C does in fact invoke the constructor of
> class A, but not of class B. i.e., only the constructor of the first
> base class is invoked. Is this a bug in the book, or a bug in Python
> 2.1?

> And if it's not a Python bug, what is the rationale for only calling
> the constructor of the first base class, and not the other base
> classes?

> ...Edmund.



More information about the Python-list mailing list