473,886 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple inheritance super()

km
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 advance for enlightment ...

here's the snippet

#!/usr/bin/python

class A(object):
def __init__(self):
self.a = 1

class B(object):
def __init__(self):
self.b = 2

class C(object):
def __init__(self):
self.c = 3

class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__( )

if __name__ == '__main__':
x = D()
print x.a # errs with - AttributeError

Jul 26 '05 #1
20 10095
km wrote:
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 advance for enlightment ...

here's the snippet

#!/usr/bin/python

class A(object):
def __init__(self):
self.a = 1

class B(object):
def __init__(self):
self.b = 2

class C(object):
def __init__(self):
self.c = 3

class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__( )


Each class should do a similar super() call, with the appropriate name
substitutions.

Calls to __init__ must be made explicitly in subclasses, including in
the case of multiple inheritance.

Also note that often (usually) you would like the __init__ call to come
*before* other location initializations , and it's the safest thing to do
unless you have clear reasons to the contrary.

-Peter
Jul 26 '05 #2
km
Hi peter,

ya got it working :-) now i understand mro better.

thanks,
KM
-------------------------------------------------------------
On Tue, Jul 26, 2005 at 04:09:55PM -0400, Peter Hansen wrote:
km wrote:
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 advance for enlightment ...

here's the snippet

#!/usr/bin/python

class A(object):
def __init__(self):
self.a = 1

class B(object):
def __init__(self):
self.b = 2

class C(object):
def __init__(self):
self.c = 3

class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__( )


Each class should do a similar super() call, with the appropriate name
substitutions.

Calls to __init__ must be made explicitly in subclasses, including in
the case of multiple inheritance.

Also note that often (usually) you would like the __init__ call to come
*before* other location initializations , and it's the safest thing to do
unless you have clear reasons to the contrary.

-Peter
--
http://mail.python.org/mailman/listinfo/python-list


Jul 26 '05 #3
Peter Hansen wrote:
km wrote:
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 ?
[snip] Each class should do a similar super() call, with the appropriate name
substitutions. [snip] -Peter


A related question is about the order of the __init__ calls. Considering
the following sample:

#--8<---
class A (object):
def __init__ (self):
super (A, self) .__init__ ()
print 'i am an A'
def foo (self):
print 'A.foo'

class B (object):
def __init__ (self):
super (B, self) .__init__ ()
print 'i am a B'
def foo (self):
print 'B.foo'

class C (A, B):
def __init__ (self):
super (C, self) .__init__ ()
print 'i am a C'

c = C ()
c.foo ()
#--8<---

aerts $ python2.4 inheritance.py
i am a B
i am an A
i am a C
A.foo

I do understand the lookup for foo: foo is provided by both classes A
and B and I do not state which one I want to use, so it takes the first
one in the list of inherited classes (order of the declaration). However
I cannot find an explanation (I may have googled the wrong keywords) for
the order of the __init__ calls from C. I was expecting (following the
same order as the method lookup):

i am an A
i am a B
i am a C
A.foo

Thanks

--
rafi

"Imaginatio n is more important than knowledge."
(Albert Einstein)
Jul 26 '05 #4
rafi wrote:
A related question is about the order of the __init__ calls. Considering
the following sample:

#--8<---
class A (object):
def __init__ (self):
super (A, self) .__init__ ()
print 'i am an A'
class B (object):
def __init__ (self):
super (B, self) .__init__ ()
print 'i am a B'
class C (A, B):
def __init__ (self):
super (C, self) .__init__ ()
print 'i am a C'
c = C ()

aerts $ python2.4 inheritance.py
i am a B
i am an A
i am a C

I do understand the lookup for foo: foo is provided by both classes A
and B and I do not state which one I want to use, so it takes the first
one in the list of inherited classes (order of the declaration). However
I cannot find an explanation (I may have googled the wrong keywords) for
the order of the __init__ calls from C. I was expecting (following the
same order as the method lookup):


This should make it clear:
class A (object):
def __init__ (self):
print '<A>',
super (A, self) .__init__ ()
print '</A>'
class B (object):
def __init__ (self):
print '<B>',
super (B, self) .__init__ ()
print '</B>'
class C (A, B):
def __init__ (self):
print '<C>',
super (C, self) .__init__ ()
print '</C>'

C()

--Scott David Daniels
Sc***********@A cm.Org
Jul 27 '05 #5
On Wed, 27 Jul 2005 12:44:12 +0530, km <km@mrna.tn.nic .in> wrote:
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 advance for enlightment ...

here's the snippet

#!/usr/bin/python

class A(object):
def __init__(self):
self.a = 1

class B(object):
def __init__(self):
self.b = 2

class C(object):
def __init__(self):
self.c = 3

class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__( )

if __name__ == '__main__':
x = D()
print x.a # errs with - AttributeError


super(D, self) is going to find __init__ in the first base in the mro where it's
defined. So x.b will be defined, but not x.a.

I don't know what you are defining, but you could call the __init__ methods
of all the base classes by something like (untested)

for base in D.mro()[1:]:
if '__init__' in vars(base): base.__init__(s elf)

replacing your super line above in class D, but I would be leery of
using __init__ methods that way unless I had a really good rationale.

Regards,
Bengt Richter
Jul 27 '05 #6
Scott David Daniels wrote:
I do understand the lookup for foo: foo is provided by both classes A
and B and I do not state which one I want to use, so it takes the
first one in the list of inherited classes (order of the declaration).
However
I cannot find an explanation (I may have googled the wrong keywords)
for the order of the __init__ calls from C. I was expecting (following
the same order as the method lookup):

This should make it clear:
class A (object):
def __init__ (self):
print '<A>',
super (A, self) .__init__ ()
print '</A>'
class B (object):
def __init__ (self):
print '<B>',
super (B, self) .__init__ ()
print '</B>'
class C (A, B):
def __init__ (self):
print '<C>',
super (C, self) .__init__ ()
print '</C>'

C()


Gosh, based on your code I added an attribute foo on both A and B, and I
now understand... The super goes through all the super classes init to
find the attributes that may have name conflict and keep the value of
the attribute baesd upon the order of the class declaration in the
definition of C (here the value of foo in A). Am I right? I am mostly
using old style (without type unification) init but this motivate the
shift for the new style. Is there somewhere a document about this?

Thanks a lot Scott

--
rafi

"Imaginatio n is more important than knowledge."
(Albert Einstein)
Jul 27 '05 #7
>I am mostly
using old style (without type unification) init but this motivate the
shift for the new style. Is there somewhere a document about this?


Yes, see http://www.python.org/2.3/mro.html by yours truly

Michele Simionato

Jul 27 '05 #8
Michele Simionato wrote:
I am mostly
using old style (without type unification) init but this motivate the
shift for the new style. Is there somewhere a document about this?


Yes, see http://www.python.org/2.3/mro.html by yours truly

Michele Simionato


Thanks a lot

--
rafi

"Imaginatio n is more important than knowledge."
(Albert Einstein)
Jul 27 '05 #9
"Michele Simionato" <mi************ ***@gmail.com> writes:
I am mostly
using old style (without type unification) init but this motivate the
shift for the new style. Is there somewhere a document about this? Yes, see http://www.python.org/2.3/mro.html by yours truly


I'd also recommend reading <URL: http://fuhm.org/super-harmful/. It's got some good practical advice on using super in your code.


<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 28 '05 #10

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

Similar topics

14
6416
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
13
3300
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution
5
448
by: newseater | last post by:
i don't know how to call methods of super classes when using multiple inheritance. I've looked on the net but with no result :( class a(object): def foo(self): print "a" class b(object): def foo(self): print "a"
22
23395
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 examples?
60
4968
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
16
2952
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. My idea was: - Load a list of plugin names (i.e. from the config file, or from the plugins directory) - Import all plugins found dynamically:
0
198
by: Scott David Daniels | last post by:
Here are some tweaks on both bits of code: Paul McGuire wrote: .... m = for b in bases: if hasattr(b, '__mro__'): if MetaHocObject.ho in b.__mro__: m.append(b) if m:
4
3951
by: Enrico | last post by:
Hi there, I have the following situation (I tryed to minimize the code to concentrate on the issue): def __getattr__(self, name): print 'A.__getattr__' if name == 'a': return 1 raise AttributeError('%s not found in A' % name) def __getattr__(self, name):
0
9956
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10770
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10429
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4627
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.