引文与描述:

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. stm单片机之STM32F4-Discovery资料汇总 (转载自http://blog.163.com/thinki_cao/blog/static/83944875201362493134992/)

    STM32F4的资料大部分都在这里: http://www.stmcu.org/download/index.php?act=ziliao&id=150 根据个人的理解对这些资料作了一些规律, ...

  2. 安卓 webview背景色的设置

    第一步:webview的xml属性设置:android:layerType = "software"(关闭webview硬件加速,颜色设置才可以起效果) 第二步:webview所在 ...

  3. BZOJ1237: [SCOI2008]配对

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1237 题目大意:你有n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一 ...

  4. iOS 开发 旧版 framework

    0. 参考 http://www.cocoachina.com/ios/20150127/11022.html http://www.cnblogs.com/gcb999/p/3296414.html ...

  5. HBuilder开发App教程04-最难搞定的是mui

    前言 前几篇说到一些HBuilder开发app的基础教程, 现在来说一下HBuilder开发app的难点,或者说是上手的难点, 就是mui, 如果你没有研究mui就贸然的上手HBuilder,那你的开 ...

  6. 位运算,算术、逻辑运算详解-java篇

    /** * 功能: * 位运算符,原码.反码.补码规则: * 1.二进制的最高位是符号位:0表示正数,1表示负数 * 2.正数的原码.反码.补码都一样 * 3.负数的反码=它的原码符号位不变,其他位取 ...

  7. angular2教程

    https://github.com/lewis617/angular2-tutorial

  8. Angular - - 脏值检查及其相关

    今天突然就想写写$digest和$apply,这些都是脏值检查的主体内容. 先以普通js来做一个简单的监控例子吧: var div = ducoment.getElementById("my ...

  9. js判断MAC地址

    function white_mac_FormCheck(mac)    {           var temp = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0- ...

  10. depcomp: line 571: exec: g++: not found

    ../depcomp: line 571: exec: g++: not foundmake[1]: *** [my_new.o] Error 127make[1]: Leaving director ...