Created
December 11, 2018 12:42
-
-
Save Z-Zheng/d8764fcf15a0e356b8ef5e4fc29a160f to your computer and use it in GitHub Desktop.
python online override class function
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 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