By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Dynamically removing methods in new-style classes

P: n/a
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

Sep 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ag********@gmail.com writes:
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:
>>x=X()
class X(object):
.... def blah(self): pass
....
>>x=X()
x.blah
<bound method X.blah of <__main__.X object at 0xb7d46bcc>>
>>del type(x).blah
x.blah
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'X' object has no attribute 'blah'
Sep 12 '07 #2

P: n/a
On Sep 12, 4:28 pm, agupta0...@gmail.com wrote:
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

Sep 12 '07 #3

P: n/a
On Wed, 12 Sep 2007 14:28:15 +0000, agupta0318 wrote:
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]

>>class C(object):
.... def method(self):
.... return "method exists"
....
>>c = C()
c.method()
'method exists'
>>del c.__class__.method
c.method()
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:

>>class C(object):
.... def method(self):
.... return "method exists"
.... def methodgone(self, *args):
.... raise AttributeError("method gone")
....
>>c = C()
d = C()
c.method = c.methodgone
c.method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in methodgone
AttributeError: method gone
>>d.method()
'method exists'
--
Steven.
Sep 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.