引文与描述:

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的更多相关文章

  1. python3新特性函数注释Function Annotations用法分析

    本文分析了python3新特性函数注释Function Annotations用法.分享给大家供大家参考,具体如下: Python 3.X新增加了一个特性(Feature),叫作函数注释 Functi ...

  2. 相比于python2.6,python3.0的新特性。

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

  3. Python3语法详解

    一.下载安装 1.1Python下载 Python官网:https://www.python.org/ 1.2Python安装 1.2.1 Linux 平台安装 以下为在Unix & Linu ...

  4. python3.X中简单错误处理,和Python2区别

    1.print 1.1 Print是一个函数 在Python3中print是个函数,这意味着在使用的时候必须带上小括号,并且它是带有参数的. >>> print 'hello wor ...

  5. Python3.0-3.6的版本变化

    Table of Contents Python3.0 简单的变化 语法的变化 新语法 改动的语法 剩下的变化 Python3.1 Python3.2 Python3.3 Python3.4 Pyth ...

  6. 简明Python3教程 9.函数

    简介 函数是程序的可复用片段,允许你为语句块赋予名字之后在程序的任何地方运行它们任意次,这称做函数调用. 我们已经使用过一些内建函数,例如len和range等. 函数也许是任何有意义的软件中最重要的构 ...

  7. 你应该知道的Python3.6、3.7、3.8新特性

    很多人在学习了基本的Python语言知识后,就转入应用阶段了,后期很少对语言本身的新变化.新内容进行跟踪学习和知识更新,甚至连已经发布了好几年的Python3.6的新特性都缺乏了解. 本文列举了Pyt ...

  8. python3  threading初体验

    python3中thread模块已被废弃,不能在使用thread模块,为了兼容性,python3将thread命名为_thread.python3中我们可以使用threading进行代替. threa ...

  9. How those spring enable annotations work--转

    原文地址:http://blog.fawnanddoug.com/2012/08/how-those-spring-enable-annotations-work.html Spring's Java ...

随机推荐

  1. jquery遍历二维数组

    function eachTowArray() {  var ar = [[1,2,3],[4,5,6],[7,8,9]];  var result="";   //结果存放变量  ...

  2. 笔记整理——C语言-http

    C语言 HTTP GZIP数据解压 - 大烧饼的实验室 - 博客园 - Google Chrome (2013/4/10 18:22:26) C语言 HTTP GZIP数据解压   这个代码在http ...

  3. AForge.NET是一个专门为开发者和研究者基于C#框架设计的视频录像

    AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...

  4. KMP算法深入解析

    本文主要介绍KMP算法原理.KMP算法是一种高效的字符串匹配算法,通过对源串进行一次遍历即可完成对字符串的匹配. 1.基础知识的铺垫 字符串T的前k(0 =< k <=tlen)个连续的字 ...

  5. java 之 Spring 框架(Java之负基础实战)

    1.Spring是什么 相当于安卓的MVC框架,是一个开源框架.一般用于轻型或中型应用. 它的核心是控制反转(IoC)和面向切面(AOP). 主要优势是分层架构,允许选择使用哪一个组件.使用基本的Ja ...

  6. Yii 1.0 基础

    骨架搭建 1.下载2.windows 创建PHP环境变量,找到php.exe的目录D:\wamp\bin\php\php5.3.5,右键我的电脑,属性\高级设置,path最后添加 ;D:\wamp\b ...

  7. jmeter接口测试实践

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  8. MySQL生产库全库备份脚本

    创建一个单独的备份用户backup,不要用root 创建备份目录 :mkdir -p /databackup/fullbackup mysql> grant SELECT,RELOAD,SHOW ...

  9. jQuery插入节点(移动节点)

    jQuery插入节点(移动节点) <%@ page language="java" import="java.util.*" pageEncoding=& ...

  10. 《JAVASCRIPT高级程序设计》错误处理与调试

    一.错误处理 错误处理在程序设计中的重要性是毋庸置疑的,任何有影响力的web应用程序都需要一套完善的错误处理机制.良好的错误机制可以让用户得到提醒,知道发生了什么事. 1.try-catch语句 tr ...