什么是变量注解? [英] What are variable annotations?

查看:52
本文介绍了什么是变量注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.6 即将发布.PEP 494 -- Python 3.6 发布时间表 提到了 12 月底,所以我浏览了Python 3.6 的新增功能,看到他们提到了变量注释:

Python 3.6 is about to be released. PEP 494 -- Python 3.6 Release Schedule mentions the end of December, so I went through What's New in Python 3.6 to see they mention the variable annotations:

PEP 484 引入了函数参数类型注释的标准,也就是类型提示.此 PEP 为 Python 添加了用于注释变量类型(包括类变量和实例变量)的语法:

PEP 484 introduced standard for type annotations of function parameters, a.k.a. type hints. This PEP adds syntax to Python for annotating the types of variables including class variables and instance variables:

primes: List[int] = []

captain: str  # Note: no initial value!

class Starship:
     stats: Dict[str, int] = {}

就像函数注解一样,Python 解释器没有给变量注解附加任何特定的含义,只将它们存储在类或模块的特殊属性 __annotations__ 中.与静态类型语言中的变量声明相比,注释语法的目标是提供一种简单的方法,通过抽象语法树和 __annotations__ 属性为第三方工具和库指定结构化类型元数据.

Just as for function annotations, the Python interpreter does not attach any particular meaning to variable annotations and only stores them in a special attribute __annotations__ of a class or module. In contrast to 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 and libraries via the abstract syntax tree and the __annotations__ attribute.

所以从我读到的内容来看,它们是来自 Python 3.5 的类型提示的一部分,在 Python 3.5 中的类型提示是什么.

So from what I read they are part of the type hints coming from Python 3.5, described in What are Type hints in Python 3.5.

我遵循 captain: strclass Starship 示例,但不确定最后一个:How do primes: List[int] = [] 解释一下?它是否定义了一个只允许整数的空列表?

I follow the captain: str and class Starship example, but not sure about the last one: How does primes: List[int] = [] explain? Is it defining an empty list that will just allow integers?

推荐答案

:= 之间的一切都是类型提示,所以 primes> 确实被定义为 List[int],并且最初设置为一个空列表(并且 stats 最初是一个空字典,定义为 Dict[str, int]).

Everything between : and the = is a type hint, so primes is indeed defined as List[int], and initially set to an empty list (and stats is an empty dictionary initially, defined as Dict[str, int]).

List[int]Dict[str, int] 不是下一个语法的一部分,但是,它们已经在 Python 3.5 类型提示 PEP 中定义.3.6 PEP 526 – 变量注释的语法提案 定义将相同提示附加到变量的语法;在您只能将类型提示附加到带有注释的变量之前(例如 primes = [] # List[int]).

List[int] and Dict[str, int] are not part of the next syntax however, these were already defined in the Python 3.5 typing hints PEP. The 3.6 PEP 526 – Syntax for Variable Annotations proposal only defines the syntax to attach the same hints to variables; before you could only attach type hints to variables with comments (e.g. primes = [] # List[int]).

ListDict 都是 Generic 类型,表示你有一个具有特定(具体)内容的列表或字典映射.

Both List and Dict are Generic types, indicating that you have a list or dictionary mapping with specific (concrete) contents.

对于List,只有一个参数"([...] 语法中的元素),即列表中每个元素的类型.对于 Dict,第一个参数是键类型,第二个参数是值类型.所以primes列表中的所有值都是整数,而stats字典中的所有键值对是(str, int) 对,将字符串映射到整数.

For List, there is only one 'argument' (the elements in the [...] syntax), the type of every element in the list. For Dict, the first argument is the key type, and the second the value type. So all values in the primes list are integers, and all key-value pairs in the stats dictionary are (str, int) pairs, mapping strings to integers.

请参阅typing.Listtyping.Dict 定义,关于泛型的部分,以及PEP 483 – 类型提示理论.

就像函数的类型提示一样,它们的使用是可选的,也被认为是注解(前提是有一个对象可以附加它们,所以模块中的全局变量和类的属性,而不是函数中的局部变量) 您可以通过 __annotations__ 属性进行内省.您可以将任意信息附加到这些注释中,您并不仅限于键入提示信息.

Like type hints on functions, their use is optional and are also considered annotations (provided there is an object to attach these to, so globals in modules and attributes on classes, but not locals in functions) which you could introspect via the __annotations__ attribute. You can attach arbitrary info to these annotations, you are not strictly limited to type hint information.

您可能需要阅读完整提案;它包含一些超出新语法的附加功能;例如,它指定何时评估此类注释、如何内省它们以及如何将某些内容声明为类属性与实例属性.

You may want to read the full proposal; it contains some additional functionality above and beyond the new syntax; it specifies when such annotations are evaluated, how to introspect them and how to declare something as a class attribute vs. instance attribute, for example.

这篇关于什么是变量注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆