Jumping over in the class hierarchy

Raymond Hettinger python at rcn.com
Tue Aug 1 22:53:13 EDT 2006


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




More information about the Python-list mailing list