还是先看定义 duck typing,
鸭子类型是多态(polymorphism)的一种形式.在这种形式中,不管对象属于哪个,
也不管声明的具体接口是什么,只要对象实现了相应的方法,函数就可以在对象上执行操作.
即忽略对象的真正类型,转而关注对象有没有实现所需的方法、签名和语义.
duck typing
A form of polymorphism where functions
operate on any object that implements the
appropriate methods, regardless of their
classes or explicit interface declarations. Wikipedia 是这样描述 duck typing 的,
在计算机语言中, duk typing 是一个类型测试的一个具体应用.
是将对类型的检查推迟到代码运行的时候,由动态类型(dynamic typing)
或者反省(reflection)实现. duck typing 应用在通过应用规则/协议(protocol)
建立一个适合的对象 object.
'如果它走起步来像鸭子,并且叫声像鸭子, 那个它一定是一只鸭子.'
对于一般类型 normal typing, 假定一个对象的 suitability 只有该对象的类型决定.
然而,对于 duck typing 来说, 一个对象 object 的 suitability 是通过该对象是否
实现了特定的方法跟属性来决定 certain methods and properties, 而不是由该对象
的来类型决定. 注,
In computer science, reflection is the ability of a computer program to
examine,introspect, and modify its own structure and behavior at runtime. From Wikipedia,
In computer programming, duck typing is an application of the duck test
in type safety.It requires that type checking be deferred to runtime,
and is implemented by means of dynamic typing or reflection.
Duck typing is concerned with establishing the suitability of an object
for some purpose, using the principle, "If it walks like a duck and it
quacks like a duck, then it must be a duck." With normal typing,
suitability is assumed to be determined by an object's type only.
In duck typing, an object's suitability is determined by the presence
of certain methods and properties (with appropriate meaning),
rather than the actual type of the object. 鸭子类型的起源 Origins of duck-typing,
现在谷歌工程师,Python 社区重要贡献者之一: Alex Martelli 说到,
我相信是 Ruby 社区推动了 duck typing 这个术语的流行.
但是这个duck typing 这种表达在 Ruby 和 Python 火之前,
就是在Python 的讨论中使用过. 根据 Wikipedia, duck typing 这一术语最早被 Alex Martelli 在 2000 所使用.
Related Link of Wikipedia - https://en.wikipedia.org/wiki/Duck_typing 归功于 python 的 数据类型 data model, 你的用户自定义类型的行为可以像 built-in 类型一样自然。
这并不需要通过继承 inheritance 来获得. 本着 duck typing, 可以在对象中只实现需要的方法, 就能
保证保证对象的行为符合预期. 对 Python 来说,这基本上是指避免使用 isinstance 检查对象的类,
更别提 type(foo) is bar 这种更糟的检查方式了,这样做没有任何好处,甚至禁止最简单的继承方式.
具体使用时,上述建议有一个常见的例外:有些 Python API 接受一个字符串或字符串序列;
如果只有一个字符串,可以把它放到列表中,从而简化处理. 因为字符串是序列类型,
所以为了把它和其他不可变序列区分开,最简单的方式是使用 isinstance(x, str) 检查.
另一方面,如果必须强制执行 API 契约,通常可以使用 isinstance 检查抽象基类。 在看例子之前, 先看简略一下儿 协议 protocol 相关内容,
在 Python 中创建功能完善的序列类型无需使用继承, 只需实现符合序列协议的方法.
在面向对象编程中,协议是非正式的接口,只在文档中定义,在代码中不定义.
例如,Python 的序列协议只需要 __len__ 和 __getitem__ 两个方法.
任对象/类型(A)只要使用标准的签名和语义实现了这两个方法,就能用在任何期待序列的地方,
然而A 是不是哪个类的子类无关紧要,只要提供了所需的方法即可.这就是 python 序列协议.
协议是非正式的,没有强制力,因此如果你知道类的具体使用场景,通常只需要实现一个协议的部分.
例如,为了支持迭代,只需实现 __getitem__ 方法,没必要提供 __len__方法. 经典示例, duck typing 处理一个字符串 string 或 可迭代字符串 iterable of strings
try: #
field_names = field_names.replace(',', ' ').split() #
except AttributeError: #
pass #
field_names = tuple(field_names) # #1, 假定 field_names 是一个字符串 string. EAFP, it’s easier to ask forgiveness than permission
#2, 将 field_names 中的 ',' 替换成空格 ' ' 并 split, 将结果放到 list 中
#3, sorry, field_names 并不像一个 str, field_names 不能 .replace 或者 .replace 后返回的结果不能 .split()
#4, 这里我men假设 新的 field_names 是一个可迭代对象
#5, 确保新的 field_names 是一个可迭代对象, 同事保存一个 copy - create 一个 tuple field_names = 'abc' #
field_names = 'A,B,C' #
try:
field_names = field_names.replace(',', ' ').split()
except AttributeError:
pass
print(field_names)
field_names = tuple(field_names)
print(field_names)
for item in field_names:
print(item) Output,
['abc'] #
('abc',) #
abc #
--------------
['A', 'B', 'C'] #
('A', 'B', 'C') #
A #
B #
C # 结论,
Summarize, Outside of frameworks, duck typing is often sim‐pler and more flexible than type checks.

鸭子类型 - Duck Typing的更多相关文章

  1. 鸭子类型(Duck Typing)

    鸭子类型(Duck Typing) 动态类型.没有类型检验.不关注类型,关注方法 相当于静态类型语言的多态 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby ...

  2. 鸭子类型duck typing(动态)

    在程序设计中,鸭子类型(duck typing)是动态类型的一种风格.在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由当前方法和属性的集合决定.这个概念的名字来源于由Ja ...

  3. 什么是鸭子类型(duck typing)

    "当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...

  4. 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.

    Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...

  5. python鸭子类型(duck type)

    1.什么是鸭子类型顾名思义,就是看起来像鸭子的类型,就可以叫做鸭子类型所以,鸭子类型是一种不严格的类型,弱类型有相同方法的类型,可以归为一类鸭子.2.鸭子类型示例 class dog: def run ...

  6. 多态 与 鸭子类型 duck duck duck

    # --> ''' 多态 与 鸭子类型 --> 什么是多态 对象的多种状态,父类对象的多种 (子类对象) 状态 --> 什么是鸭子类型: 长的像就是 1.规定有什么属性及什么方法的对 ...

  7. Python 中的Duck Typing

    在学习Python的时候发现了鸭 子类型这个术语,听起来好像很有意思一样,所以把它记下来. 鸭子类型(Duck Typing)的名字来源于"鸭子测试": "当看到一只鸟走 ...

  8. python与鸭子类型

    部分参考来源:作者:JasonDing  https://www.jianshu.com/p/650485b78d11##s1 首先介绍下面向对象(OOP)的三大特征: (1)面向对象程序设计有三大特 ...

  9. Python - 协议和鸭子类型

    参考: Fluent_Python - P430 wiki 这里说的协议是什么?是让Python这种动态类型语言实现多态的方式. 在面向对象编程中,协议是非正式的接口,是一组方法,但只是一种文档,语言 ...

随机推荐

  1. windows创建git并连结github

    1.下载跟自己系统相对应的git版本 2.默认安装 3.绑定用户 git config --global user.name ""git config --global user. ...

  2. python检查是奇数还是偶数

    检查的依据:奇数除2余1:偶数除2无余数 num = int(input("请输入一个整数:")) if num % 2 == 1: print(num,"是奇数&quo ...

  3. 中国传统色JSON数据

    提取自中国色/colors.json 解析后存入数据库,导出插入语句chinese_colors.sql,提取码:5inu [ { "CMYK": [ 4, 5, 18, 0 ], ...

  4. nor flash之擦除和写入

    最近研究了下nor flash的掉电问题,对nor的掉电有了更多的认识.总结分享如下 擦除从0变1,写入从1变0 nor flash的物理特性是,写入之前需要先进行擦除.擦除后数据为全0xFF,此时写 ...

  5. 【C_Language】---一份程序看懂C程序printf()的几种常用用法

    闲来继续巩固我的学习之路,今天略微整理了一下,C程序中Printf()的一些常用用法,虽然自己以前好像会,但是不够系统,今天大致整理了一些,先贴上来看看,以后在看到其他,继续补充,希望能帮到一些像我一 ...

  6. fill 的用法

    博客 : http://blog.csdn.net/liuchuo/article/details/52296646 fill函数的作用是:将一个区间的元素都赋予val值.函数参数:fill(vec. ...

  7. CF 558 C

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  8. Go Web 编程之 数据库

    概述 数据库用来存储数据.只要不是玩具项目,每个项目都需要用到数据库.现在用的最多的还是 MySQL,PostgreSQL的使用也在快速增长中. 在 Web 开发中,数据库也是必须的.本文将介绍如何在 ...

  9. Ubuntu学习之路2

    由于数据越来越多,学习的也稍微多了点,故将一步一步学习到的命令和快捷键继续到这篇博客. 1. 查看隐藏文件和文件夹 Ctrl+H 2. 打开搜索管理器 Alt+F2 3. 查看系统内存 free -h ...

  10. Irrelevant Elements UVA-1635 (二项式定理)

    vjudge链接 原题链接 乍一看似乎没什么思路,但是写几个简单的例子之后规律就变得很明显. 比如当 n=5 时,每一步计算后的结果如下: a1 a1+a2 a1+2a2+a3 a1+3a2+3a3+ ...