multiple inheritance

luis.armendariz at gmail.com luis.armendariz at gmail.com
Wed Feb 15 17:42:43 EST 2006


Hi Thomas,

When an object is created, the __init__ function will be called. Since
you didn't define it in Foobar, the search path finds the __init__
function in Foo, so that's the one that is called. The second __init__
in Bar is masked since it comes second in the inheritance list..

If you want to call both constructors, then what you did is fine! The
fact that 'x' comes from the Bar object is ok too. That's what you told
python to do. What did you expect if you are setting 'x' twice? I mean,
when I type the following two statements into the interpreter:

   a = 1
   a = 2

then after those two statements execute, the value of a is
naturally 2, not 1.  Same with your 'x'.

And what is wrong with the explicit calls to each constructor? After
all, in python, explicit is better than implicit. If you don't believe
me,
type "import this" in your interpreter:


>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!




More information about the Python-list mailing list