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 ...
随机推荐
- MUI开发注意事项
mui开发注意事项,有需要的朋友可以参考下. mui是一个高性能的HTML5开发框架,从UI到效率,都在极力追求原生体验:这个框架自身有一些规则,刚接触的同学不很熟悉,特总结本文:想了解mui更详细的 ...
- 前端面试题整理(html篇)
1.Doctype作用?标准模式与兼容模式各有什么区别? <!DOCTYPE>声明位于位于HTML文档中的第一行,处于 <html> 标签之前.告知浏览器的解析器用什么文档标准 ...
- List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同
两者都可以通过 KeyValuePair<TKey,TValue> 进行遍历,并且两者可以相互转换: List<KeyValuePair<string,string>&g ...
- jquery ajax promise
$request = $.getJSON('test.php'); $request.done(process1); $request.done(process2); $request.always( ...
- 电商网站垮IDC数据备份,MySql主从同步,图片及其它数据文件的同步
原文网址:http://www.bzfshop.net/article/180.html 对一个电子商务网站而言,最宝贵的资源就是数据.服务器是很廉价的东西,即使烧了好几个也问题不大,但是用户数据如果 ...
- Git 入门 ---- Git 与 SVN 区别
一. Git 是什么? Git 是目前世界上最先进的分布式版本控制系统 二. 基础知识 有中心的 SCM(Software Configuration Management) 服 ...
- delphi中panel控件应用
delphi中的panel控件是怎么使用的?研究了很久了,还是搞不懂,只知道把它放到form上面,其他操作一律不懂了,有谁可以请教一下,如何把其他控件放到里面去呢?谢谢 提问者采纳 直接把控件放到 ...
- oracle存储过程的基本语法
1.基本结构 CREATE OR REPLACE PROCEDURE 存储过程名字( 参数1 IN NUMBER, 参数2 IN NUMBER) IS变量1 INTEGER :=0;变量2 ...
- 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现
RT,马三最近在参与一款足球游戏的开发,其中涉及到足球的各种运动轨迹和路径,比如射门的轨迹,高吊球,香蕉球的轨迹.最早的版本中马三是使用物理引擎加力的方式实现的足球各种运动,后来的版本中使用了根据物理 ...
- 安装python2.7.13-64bit & Pycharm在两个python版本之间切换
本来已经安装了32位的python27,但在使用轮廓系数评估k-means模型的优良性时,出现了内存溢出的报错.原来32为的python编译器最多只能使用4GB的内存,所以就打算换成64位的pytho ...