473,389 Members | 1,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

Resolving declaring class of a method at runtime

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
9 1319
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
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.