Simon Willison’s Weblog

Subscribe

Dynamic Python class methods

27th January 2003

Dynamically extending APIs: Mark Pilgrim demonstrates how Python’s “new” module can be used to dynamically add new methods to existing classes at run time, and shows how this can be used to increase productivity when parsing XML. I’ve long been impressed with Python’s ability to add new methods to an object at runtime just by assigning a function reference to an object property but I had no idea it was possible to do this with classes as well. If you liked that tip, Dive Into Python has in depth explanations of more obscure Python features than you can shake a stick at.

This is Dynamic Python class methods by Simon Willison, posted on 27th January 2003.

Next: Weblogs table as an ordered list

Previous: Work safe CSS

Previously hosted at http://simon.incutio.com/archive/2003/01/27/dynamicPythonClassMethods