class chain methods python

Solutions on MaxInterview for class chain methods python by the best coders in the world

showing results for - "class chain methods python"
Leonardo
22 Oct 2017
1class foo():
2    def __init__(self, kind=None):
3        self.kind = kind
4    def my_print(self):
5        print (self.kind)
6        return self
7    @property
8    def line(self):
9        self.kind = 'line'
10        return self
11    @property
12    def bar(self):
13        self.kind='bar'
14        return self
15
16a = foo()
17a.line
18a.my_print()
19a.bar
20a.my_print()
21
22assert a.kind == 'bar'
23
24b = foo()
25b.line.my_print().bar.my_print()
26assert b.kind == 'bar'
27
28c = foo().line.my_print().bar.my_print()
29assert c.kind == 'bar'
Lola
12 Oct 2019
1a = foo()
2a.line()
3a.my_print()
4a.bar()
5a.my_print()
6
7assert a.kind == 'bar'
Luigi
17 Jun 2016
1c = foo().line().my_print().bar().my_print()
2assert c.kind == 'bar'