引文与描述:

Adding arbitrary metadata annotations to Python functions and variables

说说我的体会:

类似编译的作用,能够帮助你尽早地避免错误

1. 不支持 Python2+

>>> def test_annotation_py2(a_str: str):File "<stdin>", line 1def test_annotation_py2(a_str: str):^
SyntaxError: invalid syntax

2. 代码检查,而且写的时候很容易,并且可以被 IDE 如 Pycharm 支持

3. 基本用法

>>> # all is python built-in type (single)
... def search_for(neddle: str, haystack: str) -> int:
...     offset = haystack.find(needle)
...     return offset
...
>>> # More complicated types
...
>>> # Python3.5 added the `typing` module, which both gives us a bunch of new names
... # for types, and tools to build our own types
...
>>> from typing import List
>>> def multisearch(needle: str, haystack: str) -> List[int]:
...     offset = haystack.find(needle)
...     if offset == -1:
...             return []
...     else:
...             return [offset] + multisearch(needle, haystack[offset+1:])
...
>>> # In func multisearch, we define a new type List[int], `List` is from `typeing`, `int` is python built-in type. # There are many of these -e.g. Dict[keytype, valuetype], if you need more, you can view `typing` documentation
...
>>> # A func reteurn different type, use `Union`
...
>>> from typing import Union
>>> def search_for(needle: str, haystack: str) -> Union[int, None]:
...     offset = haystack.find(needle)
...     if offset == -1:
...             return None
...     else:
...             return offset

3. 注意事项

# 使用的是 [] 而不是 (): typing.List[] 而不是 typing.List()
# 类型混合,比如返回的是 (int, None) 或者是 (int, str),那么可以写为
# typing.Tuple[int, typing.Union[str, None]] 或者
# typing.Union[typing.Tuple[int, str], typing.Tuple[int, None]]

  

有一个疑问,这样写与静态语言有什么区别?都是在运行前检查。

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. Type annotations should not be confused with variable declarations in statically typed languages. The goal of annotation syntax is to provide an easy way to specify structured type metadata for third party tools.3

参考:

1. Python type annotations

2. PEP 3107 -- Function Annotations

3. PEP 526 -- Syntax for Variable Annotations

4. 弱类型、强类型、动态类型、静态类型语言的区别是什么?

转载于:https://www.cnblogs.com/jay54520/p/6432289.html

python3 annotations相关推荐

  1. Python3中装饰器@typing.overload的使用

    typing.py的源码在:https://github.com/python/cpython/blob/main/Lib/typing.py .此模块为类型提示(Type Hints)提供运行时支持 ...

  2. python3语法-python3 标签语法有什么用?

    先贴两篇python的PEP,如下:PEP 484 -- Type Hints​www.python.orgPEP 526 -- Syntax for Variable Annotations​www ...

  3. 趣学python3(10)-函数注释方法

    函数注释方法(Python3.5中引入). def add(x1:str,x2:str)->str:return x1 + x2 print(add(15,32)) print(add('abc ...

  4. 【Python3】Tensorflow_Fasterrcnn训练自己数据集,Keras_Yolov3_GPU训练自己数据集

    文章目录 1.Tensorflow_Fasterrcnn训练自己数据集 1.1 环境塔建 1.2 用预训练好的Resnet101模型演示demo图片 1.3 用预训练好的Resnet101模型对数据进 ...

  5. python 3.6.0新语法_详解Python3.6正式版新特性

    按照Python官网上的计划,Python3.6正式版期望在2016-12-16号发布,也就是这周五.从去年的5月份开始,Python3.6版本就已经动手开发了,期间也断断续续的发布了4个Alpha版 ...

  6. python3.7官网中文官网_Python官网宣布,正式发布Python 3.7.0!

    描述 Python官网静悄悄地发布了一条大消息:正式发布 Python 3.7.0!同时发布的还有Python 3.6.6稳定版.官网刚刚更新了可下载文档,还在用Python 2.7和Python3. ...

  7. python3.7.1使用_在不影响使用python3.7.1的功能的情况下,是否可以从python代码中删除所有的ufuture_uu语句?...

    您可以在不影响功能的情况下删除那些__future__导入,但是删除它们不是必需的,并且会停止与早期python版本的兼容性.在 此外,正如@deceze在评论中所暗示的那样,其他进口商品可能有所不同 ...

  8. 你应该知道的Python3.6、3.7、3.8新特性 ,赶紧收藏!!

    一.Python3.6新特性 1.新的格式化字符串方式 新的格式化字符串方式,即在普通字符串前添加 f 或 F 前缀,其效果类似于str.format().比如 name = "red&qu ...

  9. python3.4和3.6的区别_详解Python3.6正式版新特性

    按照Python官网上的计划,Python3.6正式版期望在2016-12-16号发布,也就是这周五.从去年的5月份开始,Python3.6版本就已经动手开发了,期间也断断续续的发布了4个Alpha版 ...

  10. Python3.0 新特性

    这篇文章主要介绍了相比于python 2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动 ...

最新文章

  1. LPC1768基本输入输出GPIO使用
  2. Matlab在ubuntu上运行速度,ubuntu环境下VLFeat在MATLAB上的运行
  3. python number函数_Python3 数据类型-Number
  4. Py之textgenrnn:textgenrnn库的简介、安装、使用方法详细攻略
  5. php mysqli new 连接,php mysqli 连接数据库
  6. DCMTK:DSRDocument类的测试程序
  7. 为什么要学习python
  8. (二十九)、Java字符串中去除空格
  9. python怎么发送邮件_在Python如何使用SMTP发送邮件
  10. java中图的封装,模拟java 中地图的功能封装一个有序的地图
  11. 指向函数的指针数组(C++)
  12. day03 Python字典dict的增删查改及常用操作
  13. Visual Studio的.NET内存分配分析器解析
  14. DB2导出 mysql导入_db2数据库导入导出数据
  15. 如何在C#中将 加载、编辑WPS表格?国产控件就能搞定
  16. 好书分享、能量传递-《软技能 代码之外的生存指南》自我营销篇
  17. ArrayList和LinkedList时间、空间复杂度对比
  18. 服务器端解压rar文件多次失败问题解决
  19. 神奇的输入法——小狼毫——个性化设置
  20. 计算机图像技术及其应用,计算机图像处理技术及其应用领域

热门文章

  1. mysql8.0创建用户权限,详解mysql8.0创建用户授予权限报错解决方法
  2. 如何证明一个问题是NP-Hard或NP-Complete?
  3. 基于SSM的培训机构管理系统
  4. python语法学习第十一天--迭代器
  5. 设计模式之GOF23适配器模式
  6. Android 控件之 Date Time 组件
  7. Spring Boot @ConfigurationProperties 、@EnableConfigurationProperties、@Value 注值、 ${xxx} 占位符、Environme
  8. 阶段3 2.Spring_05.基于XML的IOC的案例1_4 注解IOC案例-把自己编写的类使用注解配置...
  9. 阶段3 2.Spring_05.基于XML的IOC的案例1_3 测试基于XML的IOC案例
  10. Spring中com.sun.proxy.$Proxy12 cannot be cast to 包名.类名错误