initializing mutable class attributes

Dan Perl dperl at rogers.com
Wed Sep 1 19:47:52 CEST 2004


"Mel Wilson" <mwilson at the-wire.com> wrote in message
news:sweNBls/KXoF089yn at the-wire.com...
> In article
<8JbZc.140699$UTP.23045 at twister01.bloor.is.net.cable.rogers.com>,
> I think the difference between:
>
>     class B (A):
>         def __init__ (self, ...)
> ...         B related stuff
>             A.__init__ (self, ...)
>
>
>     class B (A):
>         def __init__ (self, ...)
>             A.__init__ (self, ...)
> ...         B related stuff
>
>
>     class B (A):
>         def __init__ (self, ...)
> ...         B related stuff
>             A.__init__ (self, ...)
> ...         more B related stuff
>
>
> shows that it's simplest just to code what you want done..
> and that's without looking at multiple inheritance,
> or different argument lists between A's and B's __init__.

Very good point.  I would usually assume that you should initialize the
superclass's attributes first, and then the subclass's.  But that may not
always be the intent and I may just be influenced by my background in C++
and Java.  And yes, with multiple inheritance, you definitely have to know
what attributes are defined in each superclass and you may have to choose
which __init__'s to use and in what order.  It's not that C++ handles
multiple inheritance any better (that's one case where "implicit" can make
things worse).  I used to work in a big company where it was actually a rule
not to use C++ multiple inheritance (which makes sense in a way because,
even if you use it correctly, someone else may take over your code later and
may have trouble with it, so a conservative company may not want to take a
risk with that).  And Java simply solves the problem by not allowing
multiple inheritance (you can 'implement' several 'interfaces', but you can
'extend' only one 'class').

I guess that in Python, everything being public, you have to think of
attributes as being part of the public interface.  So you have to know the
attributes of a superclass in order to inherit from it.  After all, it's the
same thing with public data members in C++ and Java.  But that's why those
languages recommend to make all data members non-public.

It's interesting to compare languages like that.  There is an interesting
book, "An Introduction to Object-Oriented Programming", by Timothy Budd.  I
read the 2nd edition, a long time ago.  I see there is a 3rd edition now,
published in 2001.  It explains OO concepts and compares them over several
languages.  A little hard to read sometimes, but still interesting.  I see
that even the 3rd edition does not cover Python.  Maybe a 4th edition?

Dan





More information about the Python-list mailing list