472,374 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 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 1747
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.