Skip to content

Instantly share code, notes, and snippets.

@Zert
Created January 5, 2016 11:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Zert/4da5bbe9dbe658b92deb to your computer and use it in GitHub Desktop.
#!/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