Created
January 5, 2016 11:12
-
-
Save Zert/4da5bbe9dbe658b92deb to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import types | |
class Base(object): | |
def __init__(self): | |
self.v = 5 | |
def m(self): | |
print(self.instance) | |
return self.instance.m(self) | |
def make_decision(self, t): | |
if t == 'A': | |
self.instance = A | |
if t == 'B': | |
self.instance = B | |
return setattr( | |
self, self.instance.m.__name__, | |
types.MethodType(self.instance.m, self) | |
) | |
print(self.instance) | |
class A(Base): | |
@staticmethod | |
def m(self): | |
print("from A: {0}".format(self.v)) | |
class B(Base): | |
@staticmethod | |
def m(self): | |
print("from B: {0}".format(self.v)) | |
a = Base() | |
print(a.v) | |
print(a.__class__) | |
a.make_decision('A') | |
a.m() | |
a.make_decision('B') | |
a.m() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment