Dynamically removing methods in new-style classes

Hrvoje Niksic hniksic at xemacs.org
Wed Sep 12 10:41:08 EDT 2007


agupta0318 at 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'



More information about the Python-list mailing list