Created
November 3, 2012 10:39
-
-
Save sivagao/4006974 to your computer and use it in GitHub Desktop.
Python: strategy pattern + function2method using types.MethodType
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
# 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() |
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
# 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