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 ...
随机推荐
- 关于Android反编译
详情查看:http://blog.csdn.net/ordinaryjoe/article/details/8626010
- C语言-知识点及学习路线
最近进行了为期两周的C语言培训,把几年前学过的C语言又重新学习了一遍,然后根据学习的内容和过程,总结了一下学习路线.这是基本的C语言学习路线,相当于编程语言类的基础,再根据自己究竟是要做单片机开发,还 ...
- UVa 757 - Gone Fishing
题目大意:John有h的时间可以去钓鱼,有n湖可钓鱼,以5分钟为时间单位,每个湖初始每个单位时间可钓f条鱼,每下一个时间单位数量减少d条.同时,John只能从1号湖往后走进行钓鱼,湖之间的距离需要t个 ...
- 按ctrl + c 播放下一曲音乐
./a.out . #include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<dirent. ...
- 兼容ie6及以上和firefox等标准浏览器的表格行滑过时背景色切换的效果
一.js代码——"tablehover.js" /** *②.表格单元行滑过时高亮样式动效组件封装 *oop形式封装交互动效类 *组件说明这个组件是为 ...
- linux下两台服务器文件实时同步方案设计和实现
inux下两台服务器文件实时同步方案设计和实现 假设有如下需求: 假设两个服务器: 192.168.0.1 源服务器 有目录 /opt/test/ 192.168.0.2 目标服务器 有目录 /o ...
- easyui datagrid分页参数获取
$("#btnDataExport").click(function () { $.messager.show({ title: '导出提示', msg: '每次导出一页商户数据, ...
- MapReduce深度分析(一)
MapReduce深度分析(一) 一.数据流向分析 图为MapReduce数据流向示意图 步骤1.输入文件从HDFS流向到Mapper节点.在一般情况下,存储数据的节点就是Mapper运行的节点,不需 ...
- IIS 启用w3wp.exe调试 没有找到w3wp进程
必须条件: 在进程列表的下面,有个show processes in all sessions,把它勾上就能看到了 . VS中附加进程的方式调试IIS页面,以及设置断点无效问题解决 以前调试网站的时候 ...
- linux下的Source命令的基本功能
source命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令.注:该命令通常用命令“.”来替代.如:source .bash_rc 与 . .bas ...