473,544 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the super class via the super() function

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,c ls):
if len(cls.mro()) > 1:
return 1
else:
return None
def traverse(self):
while self.hasSuper(s elf.__class__):
#do something
print 'Did something to a %s'%self
self = super(self.__cl ass__, self)

class B(A):
pass

class C(B):
pass

c = C()

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

I get this error:
while self.hasSuper(s elf.__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... :-?
Jul 18 '05 #1
2 9580
Fernando Rodriguez <fr*@easyjob.ne t> 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
Jul 18 '05 #2
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.classif y_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.getmemb ers(c,inspect.i smethod)
#do something
c.printobj(self )
print members
print
for a in inspect.classif y_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)
('__getattribut e__', '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)
('__getattribut e__', '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)
('__getattribut e__', '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)
('__getattribut e__', '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>)

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
50346
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var = $myVar<p>"; }
2
1778
by: David MacQuigg | last post by:
I think there is a documentation error in both the Library Reference section 2.1 and the Python 2.2 Quick Reference page 19. The explanation for this function is: super( type) Returns the superclass of type. I could not get this function to work right until I realized that it is searching the entire MRO, not just the superclasses of...
0
1491
by: Delaney, Timothy C (Timothy) | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195 This is a new version of super that automatically determines which method needs to be called based on the existing stack frames. It's much nicer for writing cooperative classes. It does have more overhead, but at this stage I'm not so concerned about that - the important thing is...
10
2114
by: Chris Green | last post by:
Good day, I've done a bit of searching in the language reference and a couple pages referring the behavior of super() but I can't find any discussion of why super needs the name of the class as an argument. Feel free to point me into the bowels of google if this has been discussed to death already. super(self).method() seems like super...
9
1699
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
0
1467
by: flupke | last post by:
I have the following test code setup, trying to get the class name of a subclass in the super class. (Reason why i want this is described below) file class_name_start.py ======================== import class_name as cn obj = cn.B() obj.printclass() ========================
4
1555
by: John Salerno | last post by:
Here's some code from Python in a Nutshell. The comments are lines from a previous example that the calls to super replace in the new example: class A(object): def met(self): print 'A.met' class B(A): def met(self): print 'B.met'
4
3258
by: Noah | last post by:
Am I the only one that finds the super function to be confusing? I have a base class that inherits from object. In other words new style class: class foo (object): def __init__ (self, arg_A, arg_B): self.a = arg_A self.b = arg_B # Do I need to call __init__ on "object" base class?
10
13279
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>"; print(self.text); def __del__(self): self.text = "</BODY></HTML>"; print(self.text);
0
7438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7632
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7720
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5309
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3422
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.