473,786 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_clas s
<class __main__.Y at 0x7ff24bfc>
>>y.y.im_clas s
<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 1346
Janne Härkönen wrote:
>>>class X:
... def x(self):
... pass
...
>>>class Y(X):
... def y(self):
... pass
...
>>>y = Y()
y.x.im_cla ss
<class __main__.Y at 0x7ff24bfc>
>>>y.y.im_cla ss
<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.comwro te:
$ 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_cla ss
<class __main__.Y at 0x7ff24bfc>
>>>y.y.im_cla ss
<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 "optimizati on" 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 "optimizati on" 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 "optimizati on" 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
1882
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 understand how to fix the problem. 'declaring variables Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) Dim totalPints As Double = Convert.ToDouble(txtPints.Text)
12
1879
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 to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.