【问题标题】:How can I use Function Annotation in Python 2.7如何在 Python 2.7 中使用函数注解
【发布时间】:2017-02-06 03:45:24
【问题描述】:

我在 Ipython 中看到了这个源代码:

https://github.com/ipython/ipython/blob/e1e2e960315f0f98703f6b8b077b10c99d04d70a/IPython/core/completer.py#L314

我知道这是 python3 中一个名为Function Annotation 的新功能。

但此代码也可以在 Python 2.7 中使用。为什么?

如何在 Python 2.7 中使用Function Annotation

【问题讨论】:

标签: python python-2.7 annotations ipython


【解决方案1】:

您不能直接在 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
        ...

【讨论】:

    【解决方案2】:

    不,您不能在 Python 2.7 中使用函数注释。也就是说,函数注解的主要用途是逐步键入,这可以在 Python 2.7 中使用 cmets 完成。

    【讨论】:

    • 嗨,您能看到 Python 2.7 的示例吗?
    猜你喜欢
    • 2017-08-20
    • 2014-04-14
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多