Created
May 30, 2012 23:59
-
-
Save oza/2839671 to your computer and use it in GitHub Desktop.
monkey patch example
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
# import A | |
class A: | |
def say_hello(self): | |
print("hi!") | |
def say_goodbye(self): | |
print("goodbye!") | |
# apply monkey-patch! | |
class A(A): | |
def say_hello(self): | |
print("hello!") | |
a = A() | |
a.say_hello() # => hello! | |
a.say_goodbye() # => goodbye! | |
# import A | |
class B: | |
def say_hello(self): | |
print("hi!") | |
def say_goodbye(self): | |
print("goodbye!") | |
# re-define B with no inherit | |
class B: | |
def say_hello(self): | |
print("hello!") | |
b = B() | |
b.say_hello() # => hello! | |
b.say_goodbye() # => error because B doesn't have such a method. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment