Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Created November 30, 2014 18:23
Show Gist options
  • Save kracekumar/fdba095a048c023ef372 to your computer and use it in GitHub Desktop.
Save kracekumar/fdba095a048c023ef372 to your computer and use it in GitHub Desktop.
Inject method
In [4]: class Foo(object):
...: def __init__(self, func=None):
...: if func:
...: # Better validation is required
...: self.execute = types.MethodType(func, self)
...: def execute(self):
...: print "Normal"
...:
In [5]: def execute(self):
...: print "Injected"
...:
In [6]: f1 = Foo()
In [7]: f2 = Foo(func=execute)
In [8]: f1.execute()
Normal
In [9]: f2.execute()
Injected
@shrayasr
Copy link

Holy shit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment