super() not a panacea?

Michele Simionato michele.simionato at poste.it
Wed Feb 18 02:19:30 EST 2004


Here is another solution that you will probably like more, since it does
not require to change the hierarchy. The trick is to use a custom "super"
which ignores attribute errors:

class mysuper(super):
    def __getattribute__(self,name):
        try:
            return super.__getattribute__(self,name)
        except AttributeError: # returns a do-nothing method
            return lambda *args, **kw: None

class A(object):
    def m(self):
        mysuper(A, self).m()
        print '   A'

class B(A):
    def m(self):
        mysuper(B, self).m()
        print '   B'

class C(A):
    def m(self):
        mysuper(C, self).m()
        print '   C'

class D(B,C):
    def m(self):
        mysuper(D, self).m()
        print '   D'

class Z(object):
    def m(self):
        print '   Z'

class X(A, Z):
    def m(self):
        mysuper(X, self).m()
        print '   X'

X().m()

D().m()
print X.mro()
print D.mro()



More information about the Python-list mailing list