Skip to content

Commit 687bcb2

Browse files
committed
Add chaining method pattern
1 parent a6dbd6f commit 687bcb2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

chaining_method.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
class Person(object):
5+
6+
def __init__(self, name, action):
7+
self.name = name
8+
self.action = action
9+
10+
def do_action(self):
11+
print self.name, self.action.name,
12+
return self.action
13+
14+
class Action(object):
15+
16+
def __init__(self, name):
17+
self.name = name
18+
19+
def amount(self, val):
20+
print val,
21+
return self
22+
23+
def stop(self):
24+
print 'then stop'
25+
26+
if __name__ == '__main__':
27+
28+
move = Action('move')
29+
person = Person('Jack', move)
30+
person.do_action().amount('5m').stop()
31+
32+
#===== Output =====
33+
# Jack move 5m then stop

0 commit comments

Comments
 (0)