鸭子类型 - Duck Typing
- 还是先看定义 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的更多相关文章
- 鸭子类型(Duck Typing)
鸭子类型(Duck Typing) 动态类型.没有类型检验.不关注类型,关注方法 相当于静态类型语言的多态 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby ...
- 鸭子类型duck typing(动态)
在程序设计中,鸭子类型(duck typing)是动态类型的一种风格.在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由当前方法和属性的集合决定.这个概念的名字来源于由Ja ...
- 什么是鸭子类型(duck typing)
"当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...
- 类型检查和鸭子类型 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 ...
- python鸭子类型(duck type)
1.什么是鸭子类型顾名思义,就是看起来像鸭子的类型,就可以叫做鸭子类型所以,鸭子类型是一种不严格的类型,弱类型有相同方法的类型,可以归为一类鸭子.2.鸭子类型示例 class dog: def run ...
- 多态 与 鸭子类型 duck duck duck
# --> ''' 多态 与 鸭子类型 --> 什么是多态 对象的多种状态,父类对象的多种 (子类对象) 状态 --> 什么是鸭子类型: 长的像就是 1.规定有什么属性及什么方法的对 ...
- Python 中的Duck Typing
在学习Python的时候发现了鸭 子类型这个术语,听起来好像很有意思一样,所以把它记下来. 鸭子类型(Duck Typing)的名字来源于"鸭子测试": "当看到一只鸟走 ...
- python与鸭子类型
部分参考来源:作者:JasonDing https://www.jianshu.com/p/650485b78d11##s1 首先介绍下面向对象(OOP)的三大特征: (1)面向对象程序设计有三大特 ...
- Python - 协议和鸭子类型
参考: Fluent_Python - P430 wiki 这里说的协议是什么?是让Python这种动态类型语言实现多态的方式. 在面向对象编程中,协议是非正式的接口,是一组方法,但只是一种文档,语言 ...
随机推荐
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- ATOM插件及快捷键
xml-formatter :https://atom.io/packages/xml-formatter xml格式化工具 SHIFT-CTRL-X:快速格式化 SHIFT-CTRL-M:移除换行符 ...
- 重拾c++第三天(6):分支语句与逻辑运算符
1.逻辑运算符 && || ! 2.关系运算符优先级高于逻辑运算符 3.cctype库中好用的判断 4. ?:符号用法: 状态1?结果1:结果2 5.switch用法: switch ...
- Vs中提交了代码但是不想推送到Git中
1:首先就是我fix code 是要提交上去的,所以我就开始提交呢,但是,一看提交后,还没有推送到git就是现在下面的这个状态 上面这个是==> 这是先新增的文件,第一步.但是第一步就差推送了, ...
- vs推送git失败,修改git下config的Log
一开始写完程序套推送到Git中,然后就来了一下,下面的异常: 异常1 发布到远程存储库时遇到错误: Git failed with a fatal error. fatal: HttpRequestE ...
- .NET Core开发的iNeuOS工业互联平台,iNeuKernel物联网核心组件在Docker容器中部署。
目 录 1. 概述... 2 2. 演示信息... 2 3. 安装Docker容器... 2 4. 安装dotnet镜像... 3 5. ...
- ssm之spring+springmvc+mybatis整合初探
1.基本目录如下 2.首先是向lib中加入相应的jar包 3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...
- python property()函数:定义属性
正常情况下,类包含的属性应该是隐藏的,只允许通过类提供的方法来间接的实现对类属性的访问和操作. class Person: #构造函数 def __init__(self, name): self.n ...
- 【WPF on .NET Core 3.0】 Stylet演示项目 - 简易图书管理系统(4) - 图书列表界面
在前三章中我们完成了登录窗口, 并掌握了使用Conductor来切换窗口, 但这些其实都是在为我们的系统打基础. 而本章中我们就要开始开发系统的核心功能, 即图书管理功能了. 通过本章, 我们会接触到 ...
- 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...