Created
November 30, 2014 18:23
-
-
Save kracekumar/fdba095a048c023ef372 to your computer and use it in GitHub Desktop.
Inject method
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Holy shit!