grandparent method with super

John Clark clajo04 at mac.com
Thu Apr 5 22:55:38 CEST 2007


>> 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




More information about the Python-list mailing list