Dynamically removing methods in new-style classes

A

agupta0318

I am trying unsuccessfully to remove some methods from an instance,
based on values passed in to the constructor as in the following
example:

#===============================================================
class A(object):

def __init__(self, id):
self._id = id
return

def clean(self):
if (self._id == 1):
del self.test1
elif (self._id == 2):
del self.test2
pass
return

def test1(self):
print "in test1"
return

def test2(self):
print "in test2"
return

#=============================================================================
if (__name__ == '__main__'):
try:
a = A(1)
a.clean()
a.test2()
a.test1()
except AttributeError, exc:
import traceback
traceback.print_exc()
pass

#==================================================================
I get the following error:
Traceback (most recent call last):
File "DynamicMethods.py", line 30, in ?
a.clean()
File "DynamicMethods.py", line 11, in clean
del self.test1
AttributeError: test1

With the older python classes I could have done:
self.__class__.__dict__[''test1"] to achieve the desired result.

I appreciate any help you could provide with this.

Thanks.

Amit Gupta
 
H

Hrvoje Niksic

I am trying unsuccessfully to remove some methods from an instance,

You can't remove the method from an instance because the method is
stored in its class.
With the older python classes I could have done:
self.__class__.__dict__[''test1"] to achieve the desired result.

self.__class__.test1 still works, doesn't it? Removing methods can be
achieved the same way:
.... def blah(self): pass
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'X' object has no attribute 'blah'
 
M

Michele Simionato

I am trying unsuccessfully to remove some methods from an instance,

Usuall one does not remove methods, but wraps the object and delegates
only to
a restricted number of methods. Is this solution viable? If not, you
may be
forced to change the class of the instance, or to play trick such as

def removed_method(self):
raise NotImplementedError

self.test1 = removed_method.__get__(self)

Michele Simionato
 
S

Steven D'Aprano

I am trying unsuccessfully to remove some methods from an instance,
based on values passed in to the constructor as in the following
example:
[snip]

.... def method(self):
.... return "method exists"
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'C' object has no attribute 'method'


Of course deleting methods from the class has the disadvantage that you
delete them from the class. A better solution might be to mask them:

.... def method(self):
.... return "method exists"
.... def methodgone(self, *args):
.... raise AttributeError("method gone")
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
'method exists'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top