Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Dynamic Python class methods

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.

View blog reactions

Next: Weblogs table as an ordered list

Previous: Work safe CSS

1 comment

  1. Sweet. New also provides functions for creating classes, instances, modules, functions, and code objects. The possibilities are downright scary. :)

    Garth T Kidd - 3rd February 2003 05:51 - #

Comments are closed.

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

A django site