Resolving declaring class of a method at runtime

  • Thread starter =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?=
  • Start date
?

=?ISO-8859-1?Q?Janne_H=E4rk=F6nen?=

Hello,

Is there a simple way to resolve declaring class of a method at runtime ?

Consider this simple example:

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information..... def x(self):
.... pass
........ def y(self):
.... pass
....<class __main__.Y at 0x7ff24bfc>

What I would like to find out is the declaring class of method x, ie. class X
How to do this ?
 
B

Bjoern Schliessmann

Janne said:
... def x(self):
... pass
...
... def y(self):
... pass
...
<class __main__.Y at 0x7ff24bfc>

What I would like to find out is the declaring class of method x,
ie. class X How to do this ?

The general idea of OOP is to not have to do this. I suspect you
have a problematic design. What's the problem you try to solve?

Regards,


Björn
 
D

Duncan Booth

Janne Härkönen said:
$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.... def x(self):
... pass
...... def y(self):
... pass
...<class __main__.Y at 0x7ff24bfc>

What I would like to find out is the declaring class of method x, ie.
class X How to do this ?

For what it is worth, this should work for simple cases:
for base in getattr(K, '__mro__', (K,)+K.__bases__):
if name in base.__dict__:
return base

<class __main__.X at 0x00C4FE70>
 
S

Steven D'Aprano

Hello,

Is there a simple way to resolve declaring class of a method at runtime
?

Have you tried looking at dir(TheClass) to see what it lists?

Also helpful is TheClass.__dict__.keys().

Python has powerful introspection abilities. Learn to use them, and you
too will be able to impress your friends with your Python knowledge.

... def x(self):
... pass
...


X is an "old style" class. Most people probably shouldn't use old style
classes, for various reasons. To use new style classes, you inherit from
object:

class X(object)
 
?

=?ISO-8859-1?Q?Janne_H=E4rk=F6nen?=

Have you tried looking at dir(TheClass) to see what it lists?

This is the first thing I did, but it shows both the inherited and own
methods.
Also helpful is TheClass.__dict__.keys().

This actually does the trick, thanks for the tip!
Python has powerful introspection abilities. Learn to use them, and you
too will be able to impress your friends with your Python knowledge.

I actually use introspection very frequently, but almost always through dir().

X is an "old style" class. Most people probably shouldn't use old style
classes, for various reasons. To use new style classes, you inherit from
object:

I am also aware of old and new style classes, this was the fastest way to
type it in console :)
 
S

Steven D'Aprano

I am also aware of old and new style classes, this was the fastest way
to type it in console

Ah, an optimization huh?

So, by saving 1/2 a second by not typing "(object)" in your example code,
the consequences are that I wasted my time explaining that you should use
new style classes, and you wasted your time reading and responding to my
explanation, and now I'm wasting my time *again* telling you off for
wasting my time in the first place.

There should be a term for "optimization" techniques that actually slow
things down instead of speeding them up.
 
A

Alan

There should be a term for "optimization" techniques that actually slow
things down instead of speeding them up.

I belive those are the ones known as "premature optimizations", which
sit at the root of all evil
 
C

Chris Mellon

Ah, an optimization huh?

So, by saving 1/2 a second by not typing "(object)" in your example code,
the consequences are that I wasted my time explaining that you should use
new style classes, and you wasted your time reading and responding to my
explanation, and now I'm wasting my time *again* telling you off for
wasting my time in the first place.

There should be a term for "optimization" techniques that actually slow
things down instead of speeding them up.


pessimization, of course.
 
?

=?ISO-8859-1?Q?Janne_H=E4rk=F6nen?=

Ah, an optimization huh?

So, by saving 1/2 a second by not typing "(object)" in your example code,
the consequences are that I wasted my time explaining that you should use
new style classes, and you wasted your time reading and responding to my
explanation, and now I'm wasting my time *again* telling you off for
wasting my time in the first place.

Actually, the I did not think about old and new classes when typing the
example. So you are correct in that I have a bad habit of making old style
classes whenever I experiment with something in the console.
I am sorry to have wasted your precious time in such oafish manner.

I am still grateful for the advice though :)
 
S

Steven D'Aprano

I am sorry to have wasted your precious time in such oafish manner.

And so you should be, and as soon as I can track down your address I'll
be sending you the bill.

*wink*
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top