473,666 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

grandparent method with super

Hi,

I have a class structure as follows and I would like to invoke the
method A.m() from D.m

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
# Call A.m with super?

I have read http://www.python.org/download/releases/2.2/descrintro/ but
I am still stuck.

Thanks in advance

Martin
Apr 5 '07 #1
10 5780
On Apr 5, 3:19 pm, Martin Manns <mma...@gmx.dew rote:
Hi,

I have a class structure as follows and I would like to invoke the
method A.m() from D.m

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
# Call A.m with super?

I have readhttp://www.python.org/download/releases/2.2/descrintro/but
I am still stuck.

Thanks in advance

Martin
I'm not sure if this is what you want, but it's my best guess:

class A(object):
def m(self):
print "I'm the original"
class B(A):
def m(self):
print 'B class here'
class C(A):
def m(self):
print 'C class here'
class D(B,C):
def m(self):
x = A()
x.m()

temp = D()
temp.m()

Mike

Apr 5 '07 #2
Pretty sure you can do this:

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
A.m(self)

I don't think you want to try to use super() in this case.

-jdc

-----Original Message-----
From: py************* *************** *****@python.or g
[mailto:py****** *************** ************@py thon.org] On Behalf Of Martin
Manns
Sent: Thursday, April 05, 2007 4:20 PM
To: py*********@pyt hon.org
Subject: grandparent method with super

Hi,

I have a class structure as follows and I would like to invoke the method
A.m() from D.m

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
# Call A.m with super?

I have read http://www.python.org/download/releases/2.2/descrintro/ but I am
still stuck.

Thanks in advance

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

Apr 5 '07 #3
On Thu, 5 Apr 2007 16:33:37 -0400
"John Clark" <cl*****@mac.co mwrote:
Pretty sure you can do this:

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
A.m(self)

I don't think you want to try to use super() in this case.
That works, but when I replace A with something else, I do not get the
grandparent anymore without changing all the method calls. Basically, I
would like to call the method m in the first grandparent of D.

Martin

Apr 5 '07 #4
>Pretty sure you can do this:

class A(object):
def m(self):
class B(A):
def m(self):
class C(A):
def m(self):
class D(B,C):
def m(self):
A.m(self)

I don't think you want to try to use super() in this case.

That works, but when I replace A with something else, I do not get the
grandparent anymore
>without changing all the method calls. Basically, I would like to call the
method m in the first
>grandparent of D.

Martin
I think the problem you may run into is with the term "first grandparent" -
when you look at the method resolution order of the class D, you will find
that the MRO goes "D, C, B, A"... I think it's going to be difficult to
figure out where the "first grandparent" is in the MRO.

For example:

class A(object):
def m(self):
pass
class B(A):
def m(self):
pass
class C(B):
def m(self):
pass
class D(A):
def m(self):
pass
class E(C,D):
def m(self):
firstgrandparen t(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparen t(F,self).m() # Should call F.m
The mro for class E is going to be "E,C,B,D,A" where as the mro for class F
is going to be "F,D,C,B,A" . However, the first grandparent for E should be
B, where as the first grandparent for F should be A.

Because the MRO isn't just a depth first traversal, the term "first
grandparent" gets tricky to define...

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

Apr 5 '07 #5
On Thu, 5 Apr 2007 16:55:38 -0400
"John Clark" <cl*****@mac.co mwrote:
That works, but when I replace A with something else, I do not get
the
grandparent anymore
without changing all the method calls. Basically, I would like to
call the
method m in the first
grandparent of D.

Martin

I think the problem you may run into is with the term "first
grandparent" - when you look at the method resolution order of the
class D, you will find that the MRO goes "D, C, B, A"... I think it's
going to be difficult to figure out where the "first grandparent" is
in the MRO.

For example:

class A(object):
def m(self):
pass
class B(A):
def m(self):
pass
class C(B):
def m(self):
pass
class D(A):
def m(self):
pass
class E(C,D):
def m(self):
firstgrandparen t(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparen t(F,self).m() # Should call F.m
The mro for class E is going to be "E,C,B,D,A" where as the mro for
class F is going to be "F,D,C,B,A" . However, the first grandparent
for E should be B, where as the first grandparent for F should be A.

Because the MRO isn't just a depth first traversal, the term "first
grandparent" gets tricky to define...
Not really. The first grandparent would be the first occurrence in the
list from left to right, which satisfies the requirement that its
shortest path to the current class is 2.

The only problem: How do I get it?

Martin
Apr 5 '07 #6
En Thu, 05 Apr 2007 18:13:06 -0300, Martin Manns <mm****@gmx.dee scribió:
On Thu, 5 Apr 2007 16:55:38 -0400
"John Clark" <cl*****@mac.co mwrote:
>Because the MRO isn't just a depth first traversal, the term "first
grandparent" gets tricky to define...

Not really. The first grandparent would be the first occurrence in the
list from left to right, which satisfies the requirement that its
shortest path to the current class is 2.
The only problem: How do I get it?
Calling super() twice? F.mro()[1]? But really I don't think it's a good
idea - depending on the *other* classes in your hierarchy, what you call
"grandparen t" may be almost anyone.
If you *have* to bypass your parent, it feels like there is something
wrong in the class hierarchy.

--
Gabriel Genellina

Apr 5 '07 #7
On Apr 5, 2:13 pm, Martin Manns <mma...@gmx.dew rote:
On Thu, 5 Apr 2007 16:55:38 -0400

"John Clark" <claj...@mac.co mwrote:
>That works, but when I replace A with something else, I do not get
>the
grandparent anymore
>without changing all the method calls. Basically, I would like to
>call the
method m in the first
>grandparent of D.
>Martin
I think the problem you may run into is with the term "first
grandparent" - when you look at the method resolution order of the
class D, you will find that the MRO goes "D, C, B, A"... I think it's
going to be difficult to figure out where the "first grandparent" is
in the MRO.
For example:
class A(object):
def m(self):
pass
class B(A):
def m(self):
pass
class C(B):
def m(self):
pass
class D(A):
def m(self):
pass
class E(C,D):
def m(self):
firstgrandparen t(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparen t(F,self).m() # Should call F.m
The mro for class E is going to be "E,C,B,D,A" where as the mro for
class F is going to be "F,D,C,B,A" . However, the first grandparent
for E should be B, where as the first grandparent for F should be A.
Because the MRO isn't just a depth first traversal, the term "first
grandparent" gets tricky to define...

Not really. The first grandparent would be the first occurrence in the
list from left to right, which satisfies the requirement that its
shortest path to the current class is 2.

The only problem: How do I get it?

Martin


class E(C,D):
def m(self):
for cls in E.__mro__:
if cls != E and cls not in E.__bases__:
cls.m(self)
break
.... but it's probably better that you
rethink your class hierarchy.

--
Hope this helps,
Steven

Apr 5 '07 #8
Not really. The first grandparent would be the first occurrence in the
list from left to right, which satisfies the requirement that its shortest
path to the current class is 2.
The only problem: How do I get it?
Martin
I suppose you could do a

self.__class__. __bases__[0].__bases__[0]

(With the appropriate error handling buit in..)

But I am not sure if it's a safe assumption to depend on __bases__ returning
the classes in the order you want...

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

Apr 5 '07 #9
On 5 Apr 2007 15:05:25 -0700
at************* @gmail.com wrote:
>
class E(C,D):
def m(self):
for cls in E.__mro__:
if cls != E and cls not in E.__bases__:
cls.m(self)
break
... but it's probably better that you
rethink your class hierarchy.
After seeing the implications and the solutions, I will.

Thank you everyone for your help

Martin
Apr 5 '07 #10

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

Similar topics

9
1706
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
4
11989
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
4
6497
by: ad | last post by:
base only can call the parent 's method The result of the example below is I am Parent I am Child How can we call the grandparent's method in a inhreited class, I want the result is I am GrandParent
12
1729
by: torbs | last post by:
Hi I have a a function with several methods. For simplicity it looks a bit like this: super.prototype.aProperty="HELLO"; super.prototype.returnValue = function () { return 2;
4
2899
by: davehowey | last post by:
'Learning Python' by Lutz and Ascher (excellent book by the way) explains that a subclass can call its superclass constructor as follows: class Super: def method(self): # do stuff class Extender(Super): def method(self):
4
1716
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
3
2659
by: 7stud | last post by:
When I run the following code and call super() in the Base class's __init__ () method, only one Parent's __init__() method is called. class Parent1(object): def __init__(self): print "Parent1 init called." self.x = 10 class Parent2(object):
5
4630
by: Curious | last post by:
I'll need something like: base.base.Show(); However, this doesn't work. The grandparent class name is "BaseForm". What's the correct syntax to call the "Show" method defined in the "BaseForm" class?
1
6487
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any help will be appriciated here is the code: import java.io.*; import java.util.Scanner; public class Polynomial extends UnorderedArrayList { Scanner sc = new Scanner(System.in); //Default constructor
0
8356
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
8781
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...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
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...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
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();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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
1776
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.