473,396 Members | 2,038 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,396 software developers and data experts.

Jumping over in the class hierarchy

Hello,
I want to jump over a method in the class hierarchy, that is: If I have
class A(object), clas B(A), class C(B) and in C, I want a method to do
exactly what A does but not what B does in its reimplementation, would it
be correct to do: super(A, super(B, self)).method() in C ?
Thank you.
--
Pupeno <pu****@pupeno.com(http://pupeno.com)
Aug 1 '06 #1
4 1795
Pupeno írta:
Hello,
I want to jump over a method in the class hierarchy, that is: If I have
class A(object), clas B(A), class C(B) and in C, I want a method to do
exactly what A does but not what B does in its reimplementation, would it
be correct to do: super(A, super(B, self)).method() in C ?
Thank you.
A.method(self)

Cheers,

Laszlo

Aug 1 '06 #2
Pupeno wrote:
Hello,
I want to jump over a method in the class hierarchy, that is: If I have
class A(object), clas B(A), class C(B) and in C, I want a method to do
exactly what A does but not what B does in its reimplementation, would it
be correct to do: super(A, super(B, self)).method() in C ?
Thank you.
There's no reason to mess with super. Explicitly call A.method()

Aug 1 '06 #3
Pupeno <pu****@pupeno.comwrote:
be correct to do: super(A, super(B, self)).method() in C ?
Why do you want to use super? Is there any diamond shaped inheritance
in sight? Anyway, have you actually tried, what you suggested? Well, ...

----------------------8<------------------------------------
class A(object):
def my_method(self):
print 'in A'

class B(A):
def my_method(self):
print 'in B'
super(B, self).my_method()

class C(B):
def my_method(self):
print 'in C'
super(A, super(B, self)).my_method()

print "A..."
A().my_method()
print "B..."
B().my_method()
print "C..."
C().my_method()
---------------------->8------------------------------------

leads to

----------------------8<------------------------------------
A...
in A
B...
in B
in A
C...
in C
Traceback (most recent call last):
File "test.py", line 20, in ?
C().my_method()
File "test.py", line 13, in my_method
super(A, super(B, self)).my_method()
TypeError: super(type, obj): obj must be an instance or subtype of type
---------------------->8------------------------------------

, at least on my machine ;-)

"obj must be an instance or subtype of type". So, what's the type
of "super(B, self)"? Let's see...

----------------------8<------------------------------------
class A(object):
def my_method(self):
print 'in A'

class B(A):
def my_method(self):
print 'in B'
super(B, self).my_method()

class C(B):
def my_method(self):
print 'in C'
print type(super(B, self))

C().my_method()
---------------------->8------------------------------------

----------------------8<------------------------------------
in C
<type 'super'>
---------------------->8------------------------------------

Seems, that super is a class. In fact "help(super)" at the
interactive prompt tells you that "super(type, obj) -bound super
object; requires isinstance(obj, type)". Now, the interactive help
isn't much more help, if you don't know what super does. This isn't
the interactive help's (aka super's doc string's) fault. It's just
that the docstring would become veeeery long if it wanted to
introduce the concepts behind super.

Well, now, what does super do? Let's take a simple example. Well,
at least the simplest example super was really meant to handle:
(real) diamond shape inheritance.

----------------------8<------------------------------------
class A(object):
def my_method(self):
print 'in A'

class B(A):
def my_method(self):
print 'in B'
super(B, self).my_method()

class C(A):
def my_method(self):
print 'in C'
super(C, self).my_method()

class D(B,C):
def my_method(self):
print 'in D'
super(D, self).my_method()

obj = D()
obj.my_method()
---------------------->8------------------------------------

----------------------8<------------------------------------
in D
in B
in C
in A
---------------------->8------------------------------------

What happens here?

(1) We instantiate a D object named "obj" and call its my_method.

(2) We instantiate a super object. We tell it, that we are
"in" 'obj' and that we are interested in the next base class
after handling "D". Super "asks" obj for it's ancestry (which
is D-B-C-A). The next ancestor in this list is B. Therefore
super(D, self).my_method() delegates the call to B.my_method.

(3) We're in B.my_method now.

We instantiate a super object. We tell it, that we are
"in" 'obj' and that we are interested in the next base class
after handling "B". Super "asks" obj for it's ancestry (which
is D-B-C-A). The next ancestor in this list is C. Therefore
super(B, self).my_method() delegates the call to C.my_method.

(4) We're in C.my_method now.

We instantiate a super object. We tell it, that we are
"in" 'obj' and that we are interested in the next base class
after handling "C". Super "asks" obj for it's ancestry (which
is D-B-C-A). The next ancestor in this list is A. Therefore
super(B, self).my_method() delegates the call to A.my_method.

(5) We're in A.my_method now. Nothing more of interest will happen...

Note:

(a) C.my_method "gets called by" B.my_method, even though these
two classes don't "know" anything about each other.
(b) C.my_method would never have been called, if D used super,
but B didn't (but called its parent directly).
(c) Even though B and C don't know anything about each other,
they have to care for each other's argument lists. Well, yes,
this seems to be no problem, since a child classes method
should have "the same" argument list as the base class anyway.
Yet, this doesn't hold for __init__, so you have to handle
__init__ with special care (i.e. you should only use **kwargs and
propagate *all* parameters).
(d) "super()" *does not* "cast" a child class object to the
parent class (or something like this). "super(B, self)"
*does not* return self as "B's super class" object. This
wouldn't work correctly for diamond shapes anyway.

Now that we've understood, what super() does, you could use

----------------------8<------------------------------------
class A(object):
def my_method(self):
print 'in A'

class B(A):
def my_method(self):
print 'in B'
super(B, self).my_method()

class C(B):
def my_method(self):
print 'in C'
super(B, self).my_method()

print "A..."
A().my_method()
print "B..."
B().my_method()
print "C..."
C().my_method()
---------------------->8------------------------------------

----------------------8<------------------------------------
A...
in A
B...
in B
in A
C...
in C
in A
---------------------->8------------------------------------

, but: Is it really the super magic, that you need? As long, as
you don't have diamond shape inheritance, you can always call the
base classes method directly. And casually using super doesn't help,
either, because (see note (b)) all classes in the diamond shape *must*
use super for the magic to work. In my world, this is only worth it,
whenever I really need this magic.

So, to cut a long story short, I would just use:

----------------------8<------------------------------------
class A(object):
def my_method(self):
print 'in A'

class B(A):
def my_method(self):
print 'in B'
A.my_method(self)

class C(A):
def my_method(self):
print 'in C'
A.my_method(self)

print "A..."
A().my_method()
print "B..."
B().my_method()
print "C..."
C().my_method()
---------------------->8------------------------------------

----------------------8<------------------------------------
A...
in A
B...
in B
in A
C...
in C
in A
---------------------->8------------------------------------

I hope, this helped. FWIW, i think super is a cool tool. Like a chain
saw. Use it where appropriate, then it will be real help. I also don't
consider super() "harmful" (Google will tell you, what I'm referring
to.) I just consider it to be named misleadingly, since it does *not*
return the super class or an object re-typed to the super class. But,
then again, Python is not Java... ;-)

Cheers,
--Jan Niklas
Aug 1 '06 #4
Pupeno wrote:
I want to jump over a method in the class hierarchy, that is: If I have
class A(object), clas B(A), class C(B) and in C, I want a method to do
exactly what A does but not what B does in its reimplementation, would it
be correct to do: super(A, super(B, self)).method() in C ?
You can use __bases__ to climb the class hierarchy:

>>class A(object):
def f(self):
print 1
>>class B(A):
def f(self):
print 2
>>class C(B):
def f(self):
print 3
def g(self):
C.__bases__[0].__bases__[0].f(self) # go up two levels
>>c = C()
c.f()
3
>>c.g()
1

Raymond

Aug 2 '06 #5

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

Similar topics

7
by: John J. Lee | last post by:
I'm trying to change a base class of a big class hierarchy. The hierarchy in question is 4DOM (from PyXML). 4DOM has an FtNode class that defines __getattr__ and __setattr__ that I need to...
1
by: Stefan Seefeld | last post by:
hi there, I'v run into a little problem for which I only found an ugly workaround, so I'd like to know whether people are aware of better ways to achieve this... I'm defining a class 'Class'...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
21
by: Blue Ocean | last post by:
The reason why I ask is because I am unfamiliar with the idea of templates. It seems like it would be easier if all classes that needed something like template<class T> class Stack { ... } ...
2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
2
by: BigAbility | last post by:
can i get full class hierarchy of visual studio 2005?? especially, i want to get cotrol class hierarchy of vs.net 2005 help me...
2
by: Bror Johansson | last post by:
Hi, I have a class-hierarchy (fairly deep and fairly wide). Is there a good and general way to test an instance-object obj for having a class belonging to a certain "sub-tree" of the hierarchy...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.