Created
March 7, 2014 23:01
-
-
Save durden/9421967 to your computer and use it in GitHub Desktop.
Deleting class methods in Python..a bad idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> class Foo(object): | |
... def a(self): return 'a' | |
... | |
>>> x = Foo() | |
>>> print x.a() | |
a | |
>>> | |
>>> def b(self): | |
... return 'b' | |
... | |
>>> Foo.b = b | |
>>> | |
>>> print x.b() | |
b | |
>>> | |
>>> print x.a() | |
a | |
>>> | |
>>> del Foo.__dict__['a'] | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: 'dictproxy' object does not support item deletion | |
>>> delattr(Foo, 'b') | |
>>> | |
>>> print x.b() | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
AttributeError: 'Foo' object has no attribute 'b' | |
>>> print x.a() | |
a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment