Skip to content

Commit 40a7afc

Browse files
committed
Minor pep8 correction of the docstring.
1 parent 916b996 commit 40a7afc

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

strategy.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
1-
"""http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern-written-in-python-the-sample-in-wikipedia
2-
3-
In most of other languages Strategy pattern is implemented via creating some base strategy interface/abstract class and
4-
subclassing it with a number of concrete strategies (as we can see at http://en.wikipedia.org/wiki/Strategy_pattern),
5-
however Python supports higher-order functions and allows us to have only one class and inject functions into it's
6-
instances, as shown in this example.
1+
# http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern
2+
# -written-in-python-the-sample-in-wikipedia
3+
"""
4+
In most of other languages Strategy pattern is implemented via creating some
5+
base strategy interface/abstract class and subclassing it with a number of
6+
concrete strategies (as we can see at
7+
http://en.wikipedia.org/wiki/Strategy_pattern), however Python supports
8+
higher-order functions and allows us to have only one class and inject
9+
functions into it's instances, as shown in this example.
710
"""
811
import types
912

1013

1114
class StrategyExample:
1215
def __init__(self, func=None):
13-
self.name = 'Strategy Example 0'
16+
self.name = 'Strategy Example 0'
1417
if func is not None:
15-
self.execute = types.MethodType(func, self)
18+
self.execute = types.MethodType(func, self)
1619

17-
def execute(self):
18-
print(self.name)
20+
def execute(self):
21+
print(self.name)
1922

2023

2124
def execute_replacement1(self):
22-
print(self.name + ' from execute 1')
25+
print(self.name + ' from execute 1')
2326

2427

2528
def execute_replacement2(self):
26-
print(self.name + ' from execute 2')
29+
print(self.name + ' from execute 2')
2730

2831

2932
if __name__ == '__main__':
30-
strat0 = StrategyExample()
33+
strat0 = StrategyExample()
3134

3235
strat1 = StrategyExample(execute_replacement1)
33-
strat1.name = 'Strategy Example 1'
36+
strat1.name = 'Strategy Example 1'
3437

3538
strat2 = StrategyExample(execute_replacement2)
3639
strat2.name = 'Strategy Example 2'
3740

3841
strat0.execute()
39-
strat1.execute()
42+
strat1.execute()
4043
strat2.execute()

0 commit comments

Comments
 (0)