[Python-Dev] Problem with super() usage (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Jul 16 10:12:11 CEST 2006


For about the third time in my life, I thought I might have found a use for cooperative super calls, but I've run into another problem with the concept.

Consider:

class A(object): def m(self): print "A.m"

class B(object): def m(self): print "B.m" super(B, self).m()

class C(B, A): def m(self): print "C.m" super(C, self).m()

c = C() c.m() C.m B.m A.m

Okay so far, but... what if I want to use class B on its own, or in combination with other classes that don't have an m() method?

b = B() b.m() B.m Traceback (most recent call last): File "", line 1, in ? File "", line 4, in m AttributeError: 'super' object has no attribute 'm'

In general, how is one supposed to use super() in a class which may or may not be at the end of the mro with respect to a particular method?

The only thing I can think of is to write all my super()-using methods defensively like this:

def m(self): ... s = super(C, self) if hasattr(s, 'm'): s.m()

which seems awfully tedious.

Does the Theory of Cooperative Method Calls have anything to say about this?

-- Greg



More information about the Python-Dev mailing list