Python 类型提示指定类型的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24853923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type hinting a list of a specified type
提问by Eric W.
Using Python 3's function annotations, it is possible to specify the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDEs?
使用 Python 3 的函数注释,是否可以指定同构列表(或其他集合)中包含的项目类型,以便在 PyCharm 和其他 IDE 中进行类型提示?
A pseudo-python code example for a list of int:
int 列表的伪 python 代码示例:
def my_func(l:list<int>):
pass
I know it's possible using Docstring...
我知道可以使用 Docstring ...
def my_func(l):
"""
:type l: list[int]
"""
pass
... but I prefer the annotation style if it's possible.
...但如果可能的话,我更喜欢注释样式。
采纳答案by Eric W.
Answering my own question; the TLDR answer is NoYes.
回答我自己的问题;TLDR 的答案是No Yes。
Update 2
更新 2
In September 2015, Python 3.5 was released with support for Type Hints and includes a new typingmodule. This allows for the specification of types contained within collections. As of November 2015, JetBrains PyCharm 5.0 fully supports Python 3.5 to include Type Hints as illustrated below.
2015 年 9 月,Python 3.5 发布,支持类型提示,并包含一个新的类型模块。这允许指定集合中包含的类型。截至 2015 年 11 月,JetBrains PyCharm 5.0 完全支持 Python 3.5,包括类型提示,如下图所示。
Update 1
更新 1
As of May 2015, PEP0484 (Type Hints)has been formally accepted. The draft implementation is also available at github under ambv/typehinting.
截至 2015 年 5 月,PEP0484(类型提示)已被正式接受。实现草案也可以在github 下的 ambv/typehinting 下找到。
Original Answer
原答案
As of Aug 2014, I have confirmed that it is not possible to use Python 3 type annotations to specify types within collections (ex: a list of strings).
截至 2014 年 8 月,我已确认无法使用 Python 3 类型注释来指定集合中的类型(例如:字符串列表)。
The use of formatted docstrings such as reStructuredText or Sphinx are viable alternatives and supported by various IDEs.
使用诸如 reStructuredText 或 Sphinx 之类的格式化文档字符串是可行的替代方案,并得到各种 IDE 的支持。
It also appears that Guido is mulling over the idea of extending type annotations in the spirit of mypy: http://mail.python.org/pipermail/python-ideas/2014-August/028618.html
Guido 似乎也在考虑本着 mypy 的精神扩展类型注释的想法:http://mail.python.org/pipermail/python-ideas/2014-August/028618.html
回答by Brendan Abel
With support from the BDFL, it's almost certain now that python (probably 3.5) will provide a standardized syntax for type hints via function annotations.
在 BDFL 的支持下,现在几乎可以肯定的是,python(可能是 3.5)将通过函数注释为类型提示提供标准化的语法。
https://www.python.org/dev/peps/pep-0484/
https://www.python.org/dev/peps/pep-0484/
As referenced in the PEP, there is an experimental type-checker (kind of like pylint, but for types) called mypy that already uses this standard, and doesn't require any new syntax.
正如 PEP 中所引用的,有一个名为 mypy 的实验性类型检查器(有点像 pylint,但用于类型)已经使用了这个标准,并且不需要任何新语法。
回答by alecxe
Now that Python 3.5 is officially out, there is the Type Hints supporting module - typing
and the relevant List
"type" for the generic containers.
现在 Python 3.5 正式发布,有类型提示支持模块 -typing
以及List
通用容器的相关“类型”。
In other words, now you can do:
换句话说,现在您可以执行以下操作:
from typing import List
def my_func(l: List[int]):
pass
回答by CoreCreatives
Type comments have been added since PEP 484
自PEP 484起添加了类型注释
from . import Monitor
from typing import List, Set, Tuple, Dict
active_monitors = [] # type: List[Monitor]
# or
active_monitors: List[Monitor] = []
# bonus
active_monitors: Set[Monitor] = set()
monitor_pair: Tuple[Monitor, Monitor] = (Monitor(), Monitor())
monitor_dict: Dict[str, Monitor] = {'codename': Monitor()}
# nested
monitor_pair_list: List[Dict[str, Monitor]] = [{'codename': Monitor()}]
This is currently working for me on PyCharm with Python 3.6.4
这目前正在使用 Python 3.6.4 在 PyCharm 上为我工作