跟黃哥學(xué)python序列文章之python方法鏈(method chaining)
寫這篇文章來由,有朋友說下面這樣的代碼看不懂。
choice = raw_input("please input:\n").strip()[0].lower()
很多對于有經(jīng)驗的程序員來說,這些都不是事,
但對于初學(xué)者來說,看到這樣的語法頭有點大。
這個其實是面向?qū)ο笾蟹椒ㄦ湹母拍睢?/h2>
請看維基百科上Method chaining的定義
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.
具體在python中,請看黃哥的分析:
有的python初學(xué)者對python方法連續(xù)調(diào)用不是很清楚,像霧里看花一樣。
python一切都是對象,對象調(diào)用它的方法,如果帶返回值,放回值也是對象,
這個返回值也有方法,當(dāng)然就可以用點號調(diào)用它的方法,
如此下去,就是python方法鏈調(diào)用也。
如何設(shè)計方法鏈python代碼
# coding:utf-8
"""
如何通過學(xué)習(xí)python學(xué)會編程
https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md
黃哥python遠(yuǎn)程視頻培訓(xùn)班
https://github.com/pythonpeixun/article/blob/master/index.md
黃哥python培訓(xùn)試看視頻播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
黃哥python培訓(xùn) 咨詢qq:1465376564
"""
class Person(object):
"""方法鏈小sample"""
def name(self, value):
self.name = value
return self # 返回實例對象自己才能再調(diào)用實例對象的方法。
def work(self, value):
self.working = value
return self
def introduce(self):
print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初學(xué)者學(xué)會編程!"
person = Person()
person.name("黃哥").work("黃哥python培訓(xùn)").introduce()
php方法鏈代碼
<?php
/*
黃哥php培訓(xùn) 咨詢qq:1465376564
https://github.com/pythonpeixun/article/blob/master/php_education.md
*/
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.",教初學(xué)者學(xué)會編程!\n";
}
}
$person = new Person();
$person->setName("黃哥")->work("黃哥php培訓(xùn)")->introduce();
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.
有的python初學(xué)者對python方法連續(xù)調(diào)用不是很清楚,像霧里看花一樣。
python一切都是對象,對象調(diào)用它的方法,如果帶返回值,放回值也是對象,
這個返回值也有方法,當(dāng)然就可以用點號調(diào)用它的方法,
如此下去,就是python方法鏈調(diào)用也。
# coding:utf-8
"""
如何通過學(xué)習(xí)python學(xué)會編程
https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md
黃哥python遠(yuǎn)程視頻培訓(xùn)班
https://github.com/pythonpeixun/article/blob/master/index.md
黃哥python培訓(xùn)試看視頻播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
黃哥python培訓(xùn) 咨詢qq:1465376564
"""
class Person(object):
"""方法鏈小sample"""
def name(self, value):
self.name = value
return self # 返回實例對象自己才能再調(diào)用實例對象的方法。
def work(self, value):
self.working = value
return self
def introduce(self):
print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初學(xué)者學(xué)會編程!"
person = Person()
person.name("黃哥").work("黃哥python培訓(xùn)").introduce()
<?php
/*
黃哥php培訓(xùn) 咨詢qq:1465376564
https://github.com/pythonpeixun/article/blob/master/php_education.md
*/
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.",教初學(xué)者學(xué)會編程!\n";
}
}
$person = new Person();
$person->setName("黃哥")->work("黃哥php培訓(xùn)")->introduce();