Skip to content

Instantly share code, notes, and snippets.

@whitekid
Created October 27, 2017 17:45
Show Gist options
  • Save whitekid/c1990ff4593c821384ab00e6039b9ba2 to your computer and use it in GitHub Desktop.
Save whitekid/c1990ff4593c821384ab00e6039b9ba2 to your computer and use it in GitHub Desktop.
inject method into class
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