跟黄哥学python序列文章之python方法链(method chaining)
来源:互联网 发布:音乐剪切合并软件 mac 编辑:程序博客网 时间:2024/06/05 04:43
跟黄哥学python序列文章之python方法链(method chaining)
写这篇文章来由,有朋友说下面这样的代码看不懂。
choice = raw_input(“please input:\n”).strip()[0].lower()
很多对于有经验的程序员来说,这些都不是事,
但对于初学者来说,看到这样的语法头有点大。
这个其实是面向对象中方法链的概念。
请看维基百科上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初学者对python方法连续调用不是很清楚,像雾里看花一样。python一切都是对象,对象调用它的方法,如果带返回值,放回值也是对象, 这个返回值也有方法,当然就可以用点号调用它的方法, 如此下去,就是python方法链调用也。
如何设计方法链python代码
# coding:utf-8"""如何通过学习python学会编程https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md黄哥python远程视频培训班https://github.com/pythonpeixun/article/blob/master/index.md黄哥python培训试看视频播放地址https://github.com/pythonpeixun/article/blob/master/python_shiping.md黄哥python培训 咨询qq:1465376564"""class Person(object): """方法链小sample""" def name(self, value): self.name = value return self # 返回实例对象自己才能再调用实例对象的方法。 def work(self, value): self.working = value return self def introduce(self): print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初学者学会编程!"person = Person()person.name("黄哥").work("黄哥python培训").introduce()
php方法链代码
<?php /* 黄哥php培训 咨询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.",教初学者学会编程!\n"; } } $person = new Person(); $person->setName("黄哥")->work("黄哥php培训")->introduce();
点击黄哥python培训试看视频播放地址
黄哥python远程视频培训班
0 0
- 跟黄哥学python序列文章之python方法链(method chaining)
- method chaining 方法链
- 跟我一起学Python之五:序列
- Method chaining
- Python魔术方法-Magic Method
- 跟我一起学Python之九:字符串常用方法
- 跟我一起学Python之十一:字典方法
- Python 序列的方法
- python 序列类型方法
- python数据类型之序列
- Python数据结构之序列
- Python之序列化
- python 入门之序列
- Python序列之字符串
- Python序列之字符串
- Python序列之set
- Python数据结构之序列
- Python 魔术方法(Magic Method)
- LeetCode 301. Remove Invalid Parentheses(删除无效的括号)
- 推荐系统笔记二、矩阵分解协同过滤
- 推荐系统笔记三、基于近邻的推荐系统进阶篇
- Python-回文检测和布尔操作(and,or,not)
- 散列学习(二)
- 跟黄哥学python序列文章之python方法链(method chaining)
- raft实现
- sklearn画ROC曲线
- eclipse、ddms、android studio连接不上手机问题解决
- 【接口测试】FitNesse中文指南
- 15个顶级Java多线程面试题及回答 / Java程序员面试中的多线程问题
- A failed job change
- LeetCode 302. Smallest Rectangle Enclosing Black Pixels(最小面积矩形)
- 使用fragment实现微信界面的方法