473,395 Members | 2,253 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,395 software developers and data experts.

instance method questions

howdy,

I have created an instance method for an object using new.instancemethod. It
works great. Now the questions are:

1) how do I dynamically inspect an object to determine if it has an instance
method? (there is a class method with the same name)

2) how do I dynamically delete the instance method?

thanks,
Danny

Aug 22 '07 #1
2 1627
On Aug 22, 10:23 am, Danny <shev...@lanl.govwrote:
1) how do I dynamically inspect an object to determine if it has an instance
method? (there is a class method with the same name)
class Foo:
def foo(self):
pass

x = Foo()

import types
>>isinstance(x.foo, types.MethodType)
True
2) how do I dynamically delete the instance method?
>From the instance? You can't, you have to delete it from the class:
>>del Foo.foo
Aug 22 '07 #2
Danny wrote:
I have created an instance method for an object using new.instancemethod.
It works great. Now the questions are:

1) how do I dynamically inspect an object to determine if it has an
instance method? (there is a class method with the same name)
Why would you want to do that?

Here is a way that relies on an implementation detail of current CPython:
>>class Foo(object):
.... def foo(self): pass
.... def __init__(self):
.... import new
.... def bar(self): pass
.... self.foo = new.instancemethod(bar, self)
....
>>x = Foo()
x.foo is x.foo
True
>>del x.foo
x.foo is x.foo
False

This alternative is a little less subtle:
>>y = Foo()
"foo" in y.__dict__
True
>>del y.foo
"foo" in y.__dict__
False

Peter
Aug 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
6
by: Sarir Khamsi | last post by:
I come from a C++ background and am learning some of the details of Python's OO capability and now have some questions. Given: #!/bin/env python class A(object): _x = 10 def __init__(self):...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
7
by: Tiësto | last post by:
Hi everybody. I know everyone has said to me that this doesn't exist but I'm going to try anyway. I have an instance of ClassA and I want to duplicate that instance, creating another one that is...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
6
by: Binoy | last post by:
Hello, I am getting this error once in a while from my web sites. When I will refresh web browser, it goes off and sites are working fine. Directory structures and file names are as below- ...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
5
by: Dinsdale | last post by:
I was discussing class architecture with one of the senior developers at my new job and him and I have a similar idea on how to work with data access and class libraries. That said, our...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
tracyyun
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...
0
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,...

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.