跟黃哥學(xué)python序列文章之python方法鏈(method chaining)
寫這篇文章來由栓霜,有朋友說下面這樣的代碼看不懂告私。
choice = raw_input("please input:\n").strip()[0].lower()
很多對(duì)于有經(jīng)驗(yàn)的程序員來說度秘,這些都不是事帘不,
但對(duì)于初學(xué)者來說逆趣,看到這樣的語法頭有點(diǎn)大箕母。
這個(gè)其實(shí)是面向?qū)ο笾蟹椒ㄦ湹母拍睢?/h2>
請(qǐng)看維基百科上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中苹支,請(qǐng)看黃哥的分析:
有的python初學(xué)者對(duì)python方法連續(xù)調(diào)用不是很清楚砾隅,像霧里看花一樣。
python一切都是對(duì)象债蜜,對(duì)象調(diào)用它的方法晴埂,如果帶返回值,放回值也是對(duì)象寻定,
這個(gè)返回值也有方法儒洛,當(dāng)然就可以用點(diǎn)號(hào)調(diào)用它的方法,
如此下去狼速,就是python方法鏈調(diào)用也琅锻。
如何設(shè)計(jì)方法鏈python代碼
# coding:utf-8
"""
如何通過學(xué)習(xí)python學(xué)會(huì)編程
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 # 返回實(shí)例對(duì)象自己才能再調(diào)用實(shí)例對(duì)象的方法。
def work(self, value):
self.working = value
return self
def introduce(self):
print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初學(xué)者學(xué)會(huì)編程!"
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é)會(huì)編程!\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é)者對(duì)python方法連續(xù)調(diào)用不是很清楚砾隅,像霧里看花一樣。
python一切都是對(duì)象债蜜,對(duì)象調(diào)用它的方法晴埂,如果帶返回值,放回值也是對(duì)象寻定,
這個(gè)返回值也有方法儒洛,當(dāng)然就可以用點(diǎn)號(hào)調(diào)用它的方法,
如此下去狼速,就是python方法鏈調(diào)用也琅锻。
# coding:utf-8
"""
如何通過學(xué)習(xí)python學(xué)會(huì)編程
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 # 返回實(shí)例對(duì)象自己才能再調(diào)用實(shí)例對(duì)象的方法。
def work(self, value):
self.working = value
return self
def introduce(self):
print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初學(xué)者學(xué)會(huì)編程!"
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é)會(huì)編程!\n";
}
}
$person = new Person();
$person->setName("黃哥")->work("黃哥php培訓(xùn)")->introduce();