python typing optional_python Expected type 'Optional[(int) -> Any]' (matched generic type 'Optiona...

python 3.7

Expected type 'Optional[(int) -> Any]' (matched generic type 'Optional[(_T) -> Any]'), got '(n: SupportsAbs[_T])' -> _T' instead

Inspection info: This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Types of function parameters can be specified in docstrings or in Python 3 function annotations.

在使用sorted()函数接收一个key函数来实现自定义的排序,例如按绝对值大小排序:

print(sorted([36, 5, -12, 9, -21], key=abs))

时出现了波浪线警告:

aef4c8e6468d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

警告

暂时不知道这样写有什么问题,程序是可以执行的。

但是其中出现了一个名词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: #1

field_names = field_names.replace(',', ' ').split() #2

except AttributeError: #3

pass #4

field_names = tuple(field_names) #5

#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' #6

field_names = 'A,B,C' #7

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'] #6

('abc',) #6

abc #6

--------------

['A', 'B', 'C'] #7

('A', 'B', 'C') #7

A #7

B #7

C #7

结论,

Summarize, Outside of frameworks, duck typing is often sim‐pler and more flexible than type checks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值