summaryrefslogtreecommitdiff
path: root/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to 'decorators.py')
-rw-r--r--decorators.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/decorators.py b/decorators.py
index 7f1ff29..001563e 100644
--- a/decorators.py
+++ b/decorators.py
@@ -143,3 +143,27 @@ def locked(acquire, release):
release(self)
return wrapper
return decorator
+
+
+def monkeypatch(klass, methodname=None):
+ """Decorator extending class with the decorated function
+ >>> class A:
+ ... pass
+ >>> @monkeypatch(A)
+ ... def meth(self):
+ ... return 12
+ ...
+ >>> a = A()
+ >>> a.meth()
+ 12
+ >>> @monkeypatch(A, 'foo')
+ ... def meth(self):
+ ... return 12
+ ...
+ >>> a.foo()
+ 12
+ """
+ def decorator(func):
+ setattr(klass, methodname or func.__name__, func)
+ return func
+ return decorator