Hi All.
In a complex inheritance hierarchy, it is sometimes difficult to find
where a
method is defined. I thought it might be possible to get this info
from the
method object itself, but it looks like maybe not. Here is the test
case I tried:
class A(object):
def method():
pass
class B(A):
pass
a = A()
b = B()
print a.method
print b.method
Since B inherits method from A, I thought that printing b.method might
tell
me that the definition is in A, but no. Here's the output:
<bound method A.method of <__main__.A object at 0xb7d55e0c>>
<bound method B.method of <__main__.B object at 0xb7d55e2c>>
This in indistinguishab le from the case where B overrides method.
So, is there any way to inspect a method to see where (in what class)
it
is defined?
Thanks!
Allen 3 1353
On Jun 9, 10:28*pm, allendow...@gma il.com wrote:
Hi All.
In a complex inheritance hierarchy, it is sometimes difficult to find
where a
method is defined. *I thought it might be possible to get this info
from the
method object itself, but it looks like maybe not. *Here is the test
case I tried:
class A(object):
* * def method():
* * * * pass
class B(A):
* * pass
a = A()
b = B()
print a.method
print b.method
Since B inherits method from A, I thought that printing b.method might
tell
me that the definition is in A, but no. *Here's the output:
<bound method A.method of <__main__.A object at 0xb7d55e0c>>
<bound method B.method of <__main__.B object at 0xb7d55e2c>>
This in indistinguishab le from the case where B overrides method.
So, is there any way to inspect a method to see where (in what class)
it
is defined?
I don't know if there is other easier methods, but if you have access
to the source code, you can always add a print function.
class A(object):
def method(self):
print 'Entering A.method'
... The rest of the codes ...
class B(A):
def method(self):
print 'Entering B.method'
... The rest of the codes ...
class C(A):
pass
If you don't have access to the source code, that means you shouldn't
need to worry about it.
A rather odd thing I just noticed is this:
class A(object):
def method(self):
pass
class B(A):
def method(self):
pass
class C(A):
pass
print A.method == B.method ## False
print A.method == C.method ## True
Lie wrote:
On Jun 9, 10:28 pm, allendow...@gma il.com wrote:
>Hi All.
In a complex inheritance hierarchy, it is sometimes difficult to find where a method is defined. I thought it might be possible to get this info from the method object itself, but it looks like maybe not. Here is the test case I tried:
class A(object): def method(): pass
class B(A): pass
a = A() b = B()
print a.method print b.method
Since B inherits method from A, I thought that printing b.method might tell me that the definition is in A, but no. Here's the output:
<bound method A.method of <__main__.A object at 0xb7d55e0c>> <bound method B.method of <__main__.B object at 0xb7d55e2c>>
This in indistinguishab le from the case where B overrides method.
So, is there any way to inspect a method to see where (in what class) it is defined?
You might try the inspect module. It can give you lots of information
about a method (even including the file name and line number where it
was defined). Poke around and perhaps you can find exactly what you are
looking for.
Gary Herron
Le Monday 09 June 2008 19:03:18 al*********@gma il.com, vous avez écrit*:
Thanks Maric! That's very close to what I want, although using dir()
can be misleading because if you invoke it on class B you get all of
class A's methods as well. I took your idea and wrote it up like
this:
def find_defining_c lass(obj, meth_name):
"""find and return the class object that will provide
the definition of meth_name (as a string) if it is
invoked on obj.
"""
for ty in type(obj).mro() :
if meth_name in ty.__dict__:
return ty
return None
Cheers,
Allen
Oh ! you're just right, my first writing of this was :
for m in 'a', 'b', 'c' :
print [ t for t in type(i).mro() if m in t.__dict__ ]
which I carelessly ad wrongly rewrote using dir while posting.
Sorry.
--
_____________
Maric Michaud This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 |
last post by:
(see end of message for example code)
When an instance has a dynamically assigned instance method, deepcopy
throws a TypeError with the message "TypeError: instancemethod
expected at least 2 arguments, got 0". Tested with Python 2.3.4 on
OpenBSD and Python 2.4 on Win98; same results. Is this a bug in
deepcopy, how I dynamically assign the instance method or something
else? (See example code for how I did it.)
If you're curious as...
|
by: David |
last post by:
I have a one-line script to add an onunload event handler to the body of the
document. The script is as follows:
document.getElementsByTagName("BODY").onunload=function s() {alert("s")}
Now obviously, I put the alert("s") part in for debugging purposes, just to
make sure the error wasn't in any code I was going to be running. This line
works just fine in IE6 but in Firefox it doesn't. However, if I replace
that line with the...
|
by: Steven D'Aprano |
last post by:
Suppose I create a class with some methods:
py> class C:
.... def spam(self, x):
.... print "spam " * x
.... def ham(self, x):
.... print "ham * %s" % x
....
py> C().spam(3)
spam spam spam
|
by: RobG |
last post by:
I have a function whose parameter is a reference the element that called it:
function someFunction(el)
{
...
}
The function is assigned to the onclick event of some elements in the
HTML source:
|
by: macca |
last post by:
Hi,
I am writing a GUI application. It will have a number of user defined
controls( I plan to use/create a user defined control that will output alarm
states that the user can also select and will open a dialog to give more
detailed information) .
The number of controls on the main GUI will vary from client site to client
site. I would therefore like to populate the dialog with these controls
dynamically.
| |
by: Samuel |
last post by:
Hi,
I am building a page that makes use of user control as a templating
technique. The following is that I have in mind and it is actually working:
Root/
-- login.aspx
-- login.aspx.vb
-- UC/
----- Classic/
|
by: kjvspam |
last post by:
I have a bunch of reports that are defined as metadata in a database.
Each report has an ID and a collection of filters (e.g. filter the
report where X is between 5 and 8). I need to expose the reports
through a web service.
I could write a single web method that takes in a report ID and an
ArrayList of parameters, and returns the output for the requested
report. That would work.
But I think the generic method above is too generic. I...
|
by: karinmorena |
last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is:
Week5MortgageGUI.java:151:cannot find symbol
symbol: method allInterest(double,double,double)
Location: class Week5MortgageGUI
Week5MortgageLogic allint = logic.allInterest(amount, term, rate);
Week5MortgageGUI.java:152:cannot find symbol
symbol: method allInterest(double,double,double)
Location: class Week5MortgageGUI
|
by: ADezii |
last post by:
This week’s Tip of the Week will clearly demonstrate how you can dynamically set the Drop Down List Width of a Combo Box to the length of the longest item in its Row Source. The inspiration for this Tip came from one of our own resident Experts, mshmyob. In response to a Thread relating to this very Topic, mshmyob came up with a rather ingenious method to accomplish this task. He computed the Average Character Width of a String consisting of...
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |