Skip to content

Instantly share code, notes, and snippets.

@oza
Created May 30, 2012 23:59
Show Gist options
  • Save oza/2839671 to your computer and use it in GitHub Desktop.
Save oza/2839671 to your computer and use it in GitHub Desktop.
monkey patch example
# 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