[Python-Dev] Monkeypatching idioms -- elegant or ugly? (original) (raw)

Guido van Rossum guido at python.org
Tue Jan 15 16:37:50 CET 2008


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:

from import

@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 .

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list