python3.4和3.5有什么区别_python – 3.3和3.5中的类型提示有什么区别?

– >用于注释.

One of the use cases for annotations是类型提示.

Python 3.0添加了注释,Python 3.5通过引入类型提示,标准化功能来构建该功能.

相关的PEP(Python增强建议)是:

注释只是语法,类型提示是特定的功能.

您可以将语法用于任何您喜欢的内容,例如内联文档:

def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned":

pass

语法所做的就是将您提供的额外信息附加到函数对象:

>>> def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned":

... pass

...

>>> documentation.__annotations__

{'return': 'nothing is returned', 'arg1': 'first argument', 'self': 'the instance'}

Type Hinting规范指定了如何使用这些注释来说明每个参数应该是什么类型以及返回的内容.它是注释的特定应用,因为它定义了如何解释注释.

类型提示PEP明确声明它并不是注释的唯一用途:

Note that this PEP still explicitly does NOT prevent other uses of annotations, nor does it require (or forbid) any particular processing of annotations, even when they conform to this specification. It simply enables better coordination, as PEP 333 did for web frameworks.

类型提示仍然是完全可选的,它不是也不需要您使用它.再次引用PEP:

While the proposed typing module will contain some building blocks for runtime type checking — in particular the get_type_hints() function — third party packages would have to be developed to implement specific runtime type checking functionality, for example using decorators or metaclasses. Using type hints for performance optimizations is left as an exercise for the reader.

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

强调原作.

您可以安装typing module以向早期的Python 3.x版本添加类型提示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值