By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

Resolving declaring class of a method at runtime

P: n/a
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.
>>class X:
.... def x(self):
.... pass
....
>>class Y(X):
.... def y(self):
.... pass
....
>>y = Y()
y.x.im_class
<class __main__.Y at 0x7ff24bfc>
>>y.y.im_class
<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 ?
--
__janne
Nov 15 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Janne Härkönen wrote:
>>>class X:
... def x(self):
... pass
...
>>>class Y(X):
... def y(self):
... pass
...
>>>y = Y()
y.x.im_class
<class __main__.Y at 0x7ff24bfc>
>>>y.y.im_class
<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

--
BOFH excuse #105:

UPS interrupted the server's power

Nov 15 '07 #2

P: n/a
"Janne Härkönen" <ja**************@gmail.comwrote:
$ 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.
>>>class X:
... def x(self):
... pass
...
>>>class Y(X):
... def y(self):
... pass
...
>>>y = Y()
y.x.im_class
<class __main__.Y at 0x7ff24bfc>
>>>y.y.im_class
<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:
>>def searchclass(K, name):
for base in getattr(K, '__mro__', (K,)+K.__bases__):
if name in base.__dict__:
return base

>>searchclass(Y, 'y')
<class __main__.Y at 0x00C4FE40>
>>searchclass(Y, 'x')
<class __main__.X at 0x00C4FE70>
Nov 15 '07 #3

P: n/a
On Thu, 15 Nov 2007 13:01:27 +0200, Janne Härkönen
wrote:
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.

>>>class X:
... 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)

--
Steven.
Nov 15 '07 #4

P: n/a
On Nov 15, 2007 11:07 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
On Thu, 15 Nov 2007 13:01:27 +0200, Janne Härkönen
wrote:

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 :)

--
__janne
Nov 16 '07 #5

P: n/a
On Fri, 16 Nov 2007 19:02:25 +0200, Janne Härkönen
wrote:
>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
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.
--
Steven.
Nov 16 '07 #6

P: n/a
On Nov 16, 8:51 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
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

--
Alan

Nov 16 '07 #7

P: n/a
On Nov 16, 2007 2:51 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
On Fri, 16 Nov 2007 19:02:25 +0200, Janne Härkönen
wrote:
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

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.
Nov 16 '07 #8

P: n/a
On Nov 16, 2007 10:51 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
On Fri, 16 Nov 2007 19:02:25 +0200, Janne Härkönen
wrote:
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

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 :)
--
__janne
Nov 17 '07 #9

P: n/a
On Sat, 17 Nov 2007 09:08:48 +0200, Janne Härkönen
wrote:
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*

--
Steven.
Nov 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.