I ran into the need of monkeypatching a large number of classes (for what I think are very good reasons :-) and invented two new recipes. These don't depend on Py3k, and the second one actually works all the way back to Python 2.2. Neither of these allows monkeypatching built-in types like list. If you don't know what monkeypatching is, see see http://en.wikipedia.org/wiki/Monkey_patch. I think it's useful to share these recipes, if only to to establish whether they have been discovered before, or to decide whether they are worthy of a place in the standard library. I didn't find any relevant hits on the ASPN Python cookbook. First, a decorator to add a single method to an existing class: def monkeypatch_method(cls): def decorator(func): setattr(cls, func.__name__, func) return func return decorator To use: fromimport @monkeypatch_method( ) def (self, args): return This adds to Second, a "metaclass" to add a number of methods (or other attributes) to an existing class, using a convenient class notation: def monkeypatch_class(name, bases, namespace): assert len(bases) == 1, "Exactly one base class required" base = bases[0] for name, value in namespace.iteritems(): if name != "__metaclass__": setattr(base, name, value) return base To use: from import class ( ): __metaclass__ = monkeypatch_class def (...): ... def (...): ... ... This adds , , etc. to , and makes a local alias for .
Sunday, December 13, 2009
Python Monkeypatching recipies
I found these on comp.lang.python, posted by Guido -
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment