Method Chaining or Cascading in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person:
def name(self, name):
self.name = name
return self

def age(self, age):
self.age = age
return self

def show(self):
print "My name is", self.name, "and I am", self.age, "years old."


if __name__ == "__main__":
p = Person()
p.name("Li Xiang").age(18).show()
1
My name is Li Xiang and I am 18 years old.