Getting the super class via the super() function

Discussion in 'Python' started by Fernando Rodriguez, Nov 21, 2003.

  1. 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... :-?
     
    Fernando Rodriguez, Nov 21, 2003
    #1
    1. Advertising

  2. Fernando Rodriguez <> writes:

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

    [...]

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


    That's right, it's not. Python supports multiple inheritance -- what
    is *the* superclass?

    For new-style classes you're probably best off just looking through
    cls.__mro__.

    Cheers,
    mwh

    --
    That's why the smartest companies use Common Lisp, but lie about it
    so all their competitors think Lisp is slow and C++ is fast. (This
    rumor has, however, gotten a little out of hand. :)
    -- Erik Naggum, comp.lang.lisp
     
    Michael Hudson, Nov 21, 2003
    #2
    1. Advertising

  3. Fernando Rodriguez

    Bob Willan Guest

    On Fri, 21 Nov 2003 17:29:24 +0100, Fernando Rodriguez wrote:

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

    <snip>
    I think the code below shows how to do what you want. It only prints the
    inspect.classify_class_attrs() to show that if you really want to, you can
    determine which methods are created by each class vs. inherited from a
    super class - e.g. __init__() in C vs inherited in D, traverse() in A vs
    inherited in B, C, D.

    Bob

    [bob@localhost]$ cat xx.py
    import inspect

    class A(object):

    def printobj(self):
    print '\ninside A class object'


    def traverse(self):
    classes = inspect.getmro(self.__class__)
    print classes
    # get rid of last class, the 'object' class
    classes = classes[:-1]
    print classes
    for c in classes:
    members = inspect.getmembers(c,inspect.ismethod)
    #do something
    c.printobj(self)
    print members
    print
    for a in inspect.classify_class_attrs(c):
    print a
    print '\n\n'

    class B(A):
    bb = 0
    def printobj(self):
    print '\ninside B class object'

    class C(B):
    aa = 0
    def __init__(self):
    self.c_only = 0

    def printobj(self):
    print '\ninside C class object'

    class D(C):
    def printobj(self):
    print '\ninside D class object'

    d = D()

    d.traverse()

    [bob@localhost]$ python xx.py
    (<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
    (<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>)

    inside D class object
    [('__init__', <unbound method D.__init__>), ('printobj', <unbound method D.printobj>), ('traverse', <unbound method D.traverse>)]

    ('__class__', 'data', <type 'object'>, <attribute '__class__' of 'object' objects>)
    ('__delattr__', 'method', <type 'object'>, <slot wrapper '__delattr__' of 'object' objects>)
    ('__dict__', 'data', <class '__main__.A'>, <attribute '__dict__' of 'A' objects>)
    ('__doc__', 'data', <class '__main__.D'>, None)
    ('__getattribute__', 'method', <type 'object'>, <slot wrapper '__getattribute__' of 'object' objects>)
    ('__hash__', 'method', <type 'object'>, <slot wrapper '__hash__' of 'object' objects>)
    ('__init__', 'method', <class '__main__.C'>, <function __init__ at 0x80df154>)
    ('__module__', 'data', <class '__main__.D'>, '__main__')
    ('__new__', 'data', <type 'object'>, <built-in method __new__ of type object at 0x400d6a40>)
    ('__reduce__', 'method', <type 'object'>, <method '__reduce__' of 'object' objects>)
    ('__repr__', 'method', <type 'object'>, <slot wrapper '__repr__' of 'object' objects>)
    ('__setattr__', 'method', <type 'object'>, <slot wrapper '__setattr__' of 'object' objects>)
    ('__str__', 'method', <type 'object'>, <slot wrapper '__str__' of 'object' objects>)
    ('__weakref__', 'data', <class '__main__.A'>, <member '__weakref__' of 'A' objects>)
    ('aa', 'data', <class '__main__.C'>, 0)
    ('bb', 'data', <class '__main__.B'>, 0)
    ('printobj', 'method', <class '__main__.D'>, <function printobj at 0x80c6444>)
    ('traverse', 'method', <class '__main__.A'>, <function traverse at 0x8073e54>)

    inside C class object
    [('__init__', <unbound method C.__init__>), ('printobj', <unbound method C.printobj>), ('traverse', <unbound method C.traverse>)]

    ('__class__', 'data', <type 'object'>, <attribute '__class__' of 'object' objects>)
    ('__delattr__', 'method', <type 'object'>, <slot wrapper '__delattr__' of 'object' objects>)
    ('__dict__', 'data', <class '__main__.A'>, <attribute '__dict__' of 'A' objects>)
    ('__doc__', 'data', <class '__main__.C'>, None)
    ('__getattribute__', 'method', <type 'object'>, <slot wrapper '__getattribute__' of 'object' objects>)
    ('__hash__', 'method', <type 'object'>, <slot wrapper '__hash__' of 'object' objects>)
    ('__init__', 'method', <class '__main__.C'>, <function __init__ at 0x80df154>)
    ('__module__', 'data', <class '__main__.C'>, '__main__')
    ('__new__', 'data', <type 'object'>, <built-in method __new__ of type object at 0x400d6a40>)
    ('__reduce__', 'method', <type 'object'>, <method '__reduce__' of 'object' objects>)
    ('__repr__', 'method', <type 'object'>, <slot wrapper '__repr__' of 'object' objects>)
    ('__setattr__', 'method', <type 'object'>, <slot wrapper '__setattr__' of 'object' objects>)
    ('__str__', 'method', <type 'object'>, <slot wrapper '__str__' of 'object' objects>)
    ('__weakref__', 'data', <class '__main__.A'>, <member '__weakref__' of 'A' objects>)
    ('aa', 'data', <class '__main__.C'>, 0)
    ('bb', 'data', <class '__main__.B'>, 0)
    ('printobj', 'method', <class '__main__.C'>, <function printobj at 0x80df18c>)
    ('traverse', 'method', <class '__main__.A'>, <function traverse at 0x8073e54>)

    inside B class object
    [('printobj', <unbound method B.printobj>), ('traverse', <unbound method B.traverse>)]

    ('__class__', 'data', <type 'object'>, <attribute '__class__' of 'object' objects>)
    ('__delattr__', 'method', <type 'object'>, <slot wrapper '__delattr__' of 'object' objects>)
    ('__dict__', 'data', <class '__main__.A'>, <attribute '__dict__' of 'A' objects>)
    ('__doc__', 'data', <class '__main__.B'>, None)
    ('__getattribute__', 'method', <type 'object'>, <slot wrapper '__getattribute__' of 'object' objects>)
    ('__hash__', 'method', <type 'object'>, <slot wrapper '__hash__' of 'object' objects>)
    ('__init__', 'method', <type 'object'>, <slot wrapper '__init__' of 'object' objects>)
    ('__module__', 'data', <class '__main__.B'>, '__main__')
    ('__new__', 'data', <type 'object'>, <built-in method __new__ of type object at 0x400d6a40>)
    ('__reduce__', 'method', <type 'object'>, <method '__reduce__' of 'object' objects>)
    ('__repr__', 'method', <type 'object'>, <slot wrapper '__repr__' of 'object' objects>)
    ('__setattr__', 'method', <type 'object'>, <slot wrapper '__setattr__' of 'object' objects>)
    ('__str__', 'method', <type 'object'>, <slot wrapper '__str__' of 'object' objects>)
    ('__weakref__', 'data', <class '__main__.A'>, <member '__weakref__' of 'A' objects>)
    ('bb', 'data', <class '__main__.B'>, 0)
    ('printobj', 'method', <class '__main__.B'>, <function printobj at 0x80df11c>)
    ('traverse', 'method', <class '__main__.A'>, <function traverse at 0x8073e54>)

    inside A class object
    [('printobj', <unbound method A.printobj>), ('traverse', <unbound method A.traverse>)]

    ('__class__', 'data', <type 'object'>, <attribute '__class__' of 'object' objects>)
    ('__delattr__', 'method', <type 'object'>, <slot wrapper '__delattr__' of 'object' objects>)
    ('__dict__', 'data', <class '__main__.A'>, <attribute '__dict__' of 'A' objects>)
    ('__doc__', 'data', <class '__main__.A'>, None)
    ('__getattribute__', 'method', <type 'object'>, <slot wrapper '__getattribute__' of 'object' objects>)
    ('__hash__', 'method', <type 'object'>, <slot wrapper '__hash__' of 'object' objects>)
    ('__init__', 'method', <type 'object'>, <slot wrapper '__init__' of 'object' objects>)
    ('__module__', 'data', <class '__main__.A'>, '__main__')
    ('__new__', 'data', <type 'object'>, <built-in method __new__ of type object at 0x400d6a40>)
    ('__reduce__', 'method', <type 'object'>, <method '__reduce__' of 'object' objects>)
    ('__repr__', 'method', <type 'object'>, <slot wrapper '__repr__' of 'object' objects>)
    ('__setattr__', 'method', <type 'object'>, <slot wrapper '__setattr__' of 'object' objects>)
    ('__str__', 'method', <type 'object'>, <slot wrapper '__str__' of 'object' objects>)
    ('__weakref__', 'data', <class '__main__.A'>, <member '__weakref__' of 'A' objects>)
    ('printobj', 'method', <class '__main__.A'>, <function printobj at 0x8073e1c>)
    ('traverse', 'method', <class '__main__.A'>, <function traverse at 0x8073e54>)
     
    Bob Willan, Nov 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,862
    Darryl Pierce
    Feb 24, 2005
  2. Snyke
    Replies:
    8
    Views:
    494
    Christian Decker
    Oct 4, 2004
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    490
    Michele Simionato
    Jan 15, 2004
  4. fi3nd
    Replies:
    0
    Views:
    397
    fi3nd
    Apr 8, 2010
  5. Matthew Braid

    $class::SUPER as well as $instance::SUPER?

    Matthew Braid, Aug 6, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    318
    Matthew Braid
    Aug 9, 2004
Loading...

Share This Page