python3 annotations
引文与描述:
Adding arbitrary metadata annotations to Python functions and variables
说说我的体会:
类似编译的作用,能够帮助你尽早地避免错误
1. 不支持 Python2+
>>> def test_annotation_py2(a_str: str):
File "<stdin>", line 1
def 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. 弱类型、强类型、动态类型、静态类型语言的区别是什么?
python3 annotations的更多相关文章
- python3新特性函数注释Function Annotations用法分析
本文分析了python3新特性函数注释Function Annotations用法.分享给大家供大家参考,具体如下: Python 3.X新增加了一个特性(Feature),叫作函数注释 Functi ...
- 相比于python2.6,python3.0的新特性。
这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...
- Python3语法详解
一.下载安装 1.1Python下载 Python官网:https://www.python.org/ 1.2Python安装 1.2.1 Linux 平台安装 以下为在Unix & Linu ...
- python3.X中简单错误处理,和Python2区别
1.print 1.1 Print是一个函数 在Python3中print是个函数,这意味着在使用的时候必须带上小括号,并且它是带有参数的. >>> print 'hello wor ...
- Python3.0-3.6的版本变化
Table of Contents Python3.0 简单的变化 语法的变化 新语法 改动的语法 剩下的变化 Python3.1 Python3.2 Python3.3 Python3.4 Pyth ...
- 简明Python3教程 9.函数
简介 函数是程序的可复用片段,允许你为语句块赋予名字之后在程序的任何地方运行它们任意次,这称做函数调用. 我们已经使用过一些内建函数,例如len和range等. 函数也许是任何有意义的软件中最重要的构 ...
- 你应该知道的Python3.6、3.7、3.8新特性
很多人在学习了基本的Python语言知识后,就转入应用阶段了,后期很少对语言本身的新变化.新内容进行跟踪学习和知识更新,甚至连已经发布了好几年的Python3.6的新特性都缺乏了解. 本文列举了Pyt ...
- python3 threading初体验
python3中thread模块已被废弃,不能在使用thread模块,为了兼容性,python3将thread命名为_thread.python3中我们可以使用threading进行代替. threa ...
- How those spring enable annotations work--转
原文地址:http://blog.fawnanddoug.com/2012/08/how-those-spring-enable-annotations-work.html Spring's Java ...
随机推荐
- 如何用 Git 合并两个库,并保留提交历史
转载自 https://segmentfault.com/a/1190000000678808 背景 一个中型规模项目,开始规划时就打算采用 C/S 架构,后端是单纯的 API 服务,前端在 Web ...
- PHP header( ) 禁止页面后退
header("Cache-control:no-cache,no-store,must-revalidate"); header("Pragma:no-cache&qu ...
- grunt安装详解及失败处理
标签: 1.官网 nodejs官网 https://nodejs.org/en/ Grunt官网 http://gruntjs.com/ Grunt插件首页 http://gruntjs.com/p ...
- css :active伪类的理解
/*active伪类为点击鼠标按下去还没松开鼠标的那一瞬间的事件*/
- XML 字符串解析
微信红包发送完成后返回xml字符串,解析过程如下: 1.调用解析: public ActionResult GetEntityFromXml() { string str = @"<x ...
- php常用图片处理类
<?php /** * 已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明.用imagecreate函数创建 ...
- springmvc java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
在hibernate spring springMVC整合的时候出现下面的情况: WARNING: Exception encountered during context initializatio ...
- 排查问题所用到的一些Linux命令实践(不定期更新。。)
一.前言 线上问题排查可能是每个程序员都会经历的.在排查的过程中,往往会用到很多Linux命令,也会产生一些很实用的技巧.本博文通过分析一次线上问题排查的过程,把所有用到的命令串起来.每个Linux命 ...
- HDU5878(打表)
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- [TPYBoard-Micropython之会python就能做硬件 2] 利用micropython控制NOKIA 5110屏
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.TPYboard V102板 一块 2.DS3231 ...