Skip to content

Instantly share code, notes, and snippets.

@sivagao
Created November 3, 2012 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivagao/4006974 to your computer and use it in GitHub Desktop.
Save sivagao/4006974 to your computer and use it in GitHub Desktop.
Python: strategy pattern + function2method using types.MethodType
# sivagao
# 2012年11月3日
class StrategyExample:
def __init__(self, func=None):
self.name = 'StrategyExample 0'
if func:
self.execute = types.MethodType(func, self)
def execute(self):
print(self.name)
def executeReplacement1(self):
print(self.name + 'from execute 1')
if __name__ = "__main__":
strat0 = StrategyExample()
strat1 = StrategyExample(executeReplacement1)
strat1.name = "StrategyExample 1"
strat0.execute()
strat1.execute()
# sivagao
# 2012年11月3日
class StrategyExample:
def __init__(self, func=None):
self.name = 'StrategyExample 0'
if func:
self.execute = types.MethodType(func, self)
def execute(self):
print(self.name)
def executeReplacement1(self):
print(self.name + 'from execute 1')
if __name__ = "__main__":
strat0 = StrategyExample()
strat1 = StrategyExample(executeReplacement1)
strat1.name = "StrategyExample 1"
strat0.execute()
strat1.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment