鸭子类型

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