Skip to content

Instantly share code, notes, and snippets.

@Z-Zheng
Created December 11, 2018 12:42
Show Gist options
  • Save Z-Zheng/d8764fcf15a0e356b8ef5e4fc29a160f to your computer and use it in GitHub Desktop.
Save Z-Zheng/d8764fcf15a0e356b8ef5e4fc29a160f to your computer and use it in GitHub Desktop.
python online override class function
import types
class A():
def __init__(self):
self.a = 0
def print(self, v):
print(self.a + v)
def override_print(self, fn):
self.print = types.MethodType(fn, self)
def new_print(self, value):
print(self.a + value * 2)
a = A()
a.override_print(new_print)
a.print(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment