473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

multiple inheritance

Hi.

I think I'm missing something about multiple inheritance in python.

I've got this code.

class Foo:
def __init__(self):
self.x = "defined by foo"
self.foo = None

class Bar:
def __init__(self):
self.x = "defined by bar"
self.bar = None

class Foobar(Foo,Bar):
pass

fb = Foobar()
print fb.x
print fb.__dict__

which returns :
defined by foo
{'x': 'defined by foo', 'foo': None}

So I guess not defining __init__ in my class Foobar will call __init__
from my superclass Foo. Also __dict__ doesn't show an attribute
'bar':None so I guess Bar.__init__ is not called at all.

I would like to have a subclass with all attributes from superclasses
defined, and only the attribute from the first when there is conflict
(i.e. in this case, Foobar would be like this but with bar:None)

I tried this :

class Foobar(Foo,Bar):
def __init__(self):
Foo.__init__(self)
Bar.__init__(self)

defined by bar
{'x': 'defined by bar', 'foo': None, 'bar': None}

Here I have all I want, except the value of 'x' comes from the 'Bar'
superclass rather than Foo. So, to have what I want, I would have to
invert the two calls to __init__ in order to have the right x value.

What I find awkward here is that the order of __init__ calls matters,
rather than the order of the classes in the class declaration.

Do you have any ideas of a way to get this multiple inheritance thing
solved without having to do those __init__ calls ?

Thomas

Feb 15 '06 #1
5 2791

Thomas Girod a écrit :
Hi.

I think I'm missing something about multiple inheritance in python.

I've got this code.

class Foo:
def __init__(self):
self.x = "defined by foo"
self.foo = None

class Bar:
def __init__(self):
self.x = "defined by bar"
self.bar = None

class Foobar(Foo,Bar):
pass

fb = Foobar()
print fb.x
print fb.__dict__

which returns :
defined by foo
{'x': 'defined by foo', 'foo': None}

So I guess not defining __init__ in my class Foobar will call __init__
from my superclass Foo. Also __dict__ doesn't show an attribute
'bar':None so I guess Bar.__init__ is not called at all.

I would like to have a subclass with all attributes from superclasses
defined, and only the attribute from the first when there is conflict
(i.e. in this case, Foobar would be like this but with bar:None)

I tried this :

class Foobar(Foo,Bar):
def __init__(self):
Foo.__init__(self)
Bar.__init__(self)
defined by bar
{'x': 'defined by bar', 'foo': None, 'bar': None}

Here I have all I want, except the value of 'x' comes from the 'Bar'
superclass rather than Foo. So, to have what I want, I would have to
invert the two calls to __init__ in order to have the right x value.

What I find awkward here is that the order of __init__ calls matters,
rather than the order of the classes in the class declaration.

Do you have any ideas of a way to get this multiple inheritance thing
solved without having to do those __init__ calls ?


try:

class Foo(object):
def __init__(self):
super(Foo, self).__init__() # not really necessary here
self.x = "defined by foo"

class Bar(object):
def __init__(self):
super(Bar, self).__init__()
self.x = "defined by bar"

class FooBar(Foo, Bar):
def __init__(self):
super(FooBar, self).__init__()
and search for the "cooperative methods and super" section
in http://www.python.org/2.2/descrintro.html

Cheers,

SB


Thomas


Feb 15 '06 #2
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!

Feb 15 '06 #3
Sébastien Boisgérault <Se*******************@gmail.com> wrote:
and search for the "cooperative methods and super" section
in http://www.python.org/2.2/descrintro.html


...., then read http://fuhm.org/super-harmful/ (not the evangelism, just
the examples) and
http://mail.python.org/pipermail/pyt...ry/050656.html .

When done, ask yourself if you need cooperative method calling or just
calls to the superclasses' __init__-functions. It's your call then,
because we don't know your use cases.

Cheers,
-jnf
Feb 16 '06 #4
thanks, you pointed exactly on what distrurbed me. I'll see what I can
do with cooperative methods.

Feb 16 '06 #5
That's perfect. thanks.

Feb 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
15
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.