Dynamically adding and removing methods
collinw at gmail.com
Sat Sep 24 20:33:11 CEST 2005
One caveat, as I recently discovered, to dynamically adding methods is
that it doesn't work for __foo__ methods. For example, you can't make
an object into an iterator by dynamically assigning bound methods to
obj.__iter__ and obj.next. Same thing with __getitem__, __setitem__,
etc; adding them directly to the instance doesn't get you a
subscriptable object. This is because the interpreter looks at the
methods defined by the class, rather than the instance's attributes.
Just a word of caution.
More information about the Python-list