473,379 Members | 1,257 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,379 software developers and data experts.

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 5660
On Apr 5, 3:19 pm, Martin Manns <mma...@gmx.dewrote:
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.org
[mailto:py*********************************@python. org] On Behalf Of Martin
Manns
Sent: Thursday, April 05, 2007 4:20 PM
To: py*********@python.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.comwrote:
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):
firstgrandparent(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparent(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.comwrote:
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):
firstgrandparent(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparent(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.deescribió:
On Thu, 5 Apr 2007 16:55:38 -0400
"John Clark" <cl*****@mac.comwrote:
>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
"grandparent" 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.dewrote:
On Thu, 5 Apr 2007 16:55:38 -0400

"John Clark" <claj...@mac.comwrote:
>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):
firstgrandparent(E,self).m() #Should call B.m
class F(D,C):
def m(self):
firstgrandparent(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
ky******@gmail.com a écrit :
On Apr 5, 3:19 pm, Martin Manns <mma...@gmx.dewrote:
>>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?
(snip)
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()
Err... This will call A.m(x), not A.m(self). The correct thing to is:
class D(B,C):
def m(self):
A.m(self)

But this doesn't solve the OP's problem, which is to not hard-code the
concrete base class to call (hence the question about how to do this
using super)
Apr 5 '07 #11

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

Similar topics

9
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
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
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...
12
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
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...
4
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
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...
5
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"...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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...

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.