如何在 Python 2.7 中使用函数注释 [英] How can I use Function Annotation in Python 2.7

查看:37
本文介绍了如何在 Python 2.7 中使用函数注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saw this source code in Ipython:

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

我知道这是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屋!

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