我在 Ipython 中看到了这个源代码:
我知道这是python3中一个叫做Function Annotation
的新特性。
但是这段代码也可以在 Python 2.7 中工作。为什么?
如何在 Python 2.7 中使用 Function Annotation
?
最佳答案
您不能在 2.7 中直接使用新的 (3.5+) 注释语法,但是如果您有 python 3.4+,您可以安装 mypy 并使用不同的语法在 2.7 代码上运行它。参见 PEP 484 .
您在链接中指向的示例可以在 2.7 中编写为:
class Completion:
def __init__(self, start, end, text, type=None, _origin=''):
# type: (int, int, str, str, str) -> None
...
关于python - 如何在 Python 2.7 中使用函数注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42060055/