Getting the super class via the super() function

Fernando Rodriguez frr at easyjob.net
Fri Nov 21 17:29:24 CET 2003


Hi,

I need to traverse the methods defined in a class and its superclasses.  This
is the code I'm using:

# An instance of class B should be able to check all the methods defined in B
#and A, while an instance of  class C should be able to check all methods
#defined in C, B and A.

#------------------------------------------------
class A(object):

    def hasSuper(self,cls):
        if len(cls.mro()) > 1:
            return 1
        else:
            return None
    

    def traverse(self):
        while self.hasSuper(self.__class__):
            #do something
            print 'Did something to a %s'%self
            self = super(self.__class__, self)

class B(A):
    pass

class C(B):
    pass

c = C()

c.traverse()
#-----------------------------------------------

I get this error:
   while self.hasSuper(self.__class__):
AttributeError: 'super' object has no attribute 'hasSuper'

What am I doing wrong?  I'm afraid that super isn't returning a reference to
the superclass... :-?




More information about the Python-list mailing list