如何在 Python 2.7 中使用函数注释 [英] How can I use Function Annotation in Python 2.7
问题描述
我在 Ipython 中看到了这个源代码:
I saw this source code in Ipython:
我知道这是python3中一个叫做Function Annotation
的新特性.
I know it is a new feature called Function Annotation
in python3.
但是这段代码也可以在 Python 2.7 中工作.为什么?
but this code may also work in Python 2.7. Why?
如何在 Python 2.7 中使用 Function Annotation
?
How can I use Function Annotation
in Python 2.7?
推荐答案
你不能在 2.7 中直接使用新的 (3.5+) 注释语法,但是如果你有 python 3.4+ 你可以安装 mypy 并在你的机器上运行它2.7 不同语法的代码.请参阅 PEP 484.
You can't use the new (3.5+) annotation syntax directly in 2.7, but if you have python 3.4+ you can install mypy and run it on your 2.7 code with a different syntax. See PEP 484.
您在链接中指向的示例可以在 2.7 中编写为:
The example you pointed to in your link could be written in 2.7 as:
class Completion:
def __init__(self, start, end, text, type=None, _origin=''):
# type: (int, int, str, str, str) -> None
...
这篇关于如何在 Python 2.7 中使用函数注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!