introspection: How to find out the class defining a method

H

Holger Joukl

Hi,
introspection works different in python 2.3:

Python 2.3.3 (#12, Feb 19 2004, 11:42:09)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information..... def do(self):
.... pass
........ pass
....
whereas:

Python 2.1 (#3, Jun 1 2001, 15:51:25)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information..... def do(self):
.... pass
........ pass
....
Is there an elegant way to achieve the latter result (i.e. the base class
in which
this method actually is defined) in python 2.3, too? Couldn´t find it in
the docs.

Cheers
Holger

Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.

The contents of this e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail. Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.
 
J

Jeremy Yallop

Holger said:
Python 2.1 (#3, Jun 1 2001, 15:51:25)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.... def do(self):
... pass
...... pass
...
Is there an elegant way to achieve the latter result (i.e. the base
class in which this method actually is defined) in python 2.3, too?
Couldn´t find it in the docs.

I'm not sure this qualifies as "elegant". I think it gives the right
answer when it works, though.

def class_defining_method(method):
for c in inspect.getmro(method.im_class):
if c.__dict__.has_key(method.im_func.__name__):
return c

Jeremy.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top