473,320 Members | 1,978 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,320 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 1316
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.