Created
October 27, 2017 17:45
-
-
Save whitekid/c1990ff4593c821384ab00e6039b9ba2 to your computer and use it in GitHub Desktop.
inject method into class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
def inject(klass): | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
return func(*args, **kwargs) | |
setattr(klass, func.__name__, func) | |
return wrapper | |
return decorator | |
class Foo: | |
pass | |
@inject(Foo) | |
def foo_hello(self): | |
print('hello') | |
Foo().foo_hello() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment