Python method chaining

Write this article from the reasons that a friend said the following code can not understand.

choice = raw_input (“Please enter: \ n”). strip () [0] .lower ()

Many of the experienced programmers, these are not things,

But for beginners, to see such a grammar head a bit big.

This is actually the concept of object-oriented method chain.

See Wikipedia definition of the method chain

Method chaining, also known as named parameter idiom,   
is a common syntax for invoking multiple method calls
in object-oriented programming languages.
Each method returns an object, allowing the calls
to be chained together in a single statement without requiring
variables to store the intermediate results.
Local variable declarations are syntactic
sugar because of the difficulty humans have with deeply nested method calls.
A method chain is also known as a train wreck due to the increase
in the number of methods that come one after another in the same
line that occurs as more methods are chained together
even though line breaks are often added between methods.

Specifically in python, see the analysis of the brotherhuang:

Some python beginners on the python method is not very clear call, like a fog like a flower.

python everything is the object, the object calls its method, if the return value, the return value is the object,

This return value also has a method, of course, you can use the point number to call its method,

So go on, is the python method chain call also.

How to design method chain python code

# coding:utf-8
黄哥python培训 咨询qq:1465376564

class Person(object):

def name(self, value): = value
return self # 返回实例对象自己才能再调用实例对象的方法。

def work(self, value):
self.working = value
return self

def introduce(self):
print "你好, 我的名字:",, ",我的工作:", self.working, ",教初学者学会编程!"

person = Person()"黄哥").work("黄哥python培训").introduce()


黄哥php培训 咨询qq:1465376564

class Person{
public $name;
public $working;

public function setName($value){
$this->name = $value;
return $this;

public function work($value){
$this->working = $value;
return $this;

public function introduce(){
echo "你好, 我的名字:".$this->name.",我的工作:".$this->working.",教初学者学会编程!\n";

$person = new Person();
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.