首页 > 解决方案 > python2中的函数注释

问题描述

python2.7中可以使用函数注解吗?

即这样的代码在python2.7中产生无效的语法,但在python3.5中有效

def apply(self, img_bgr) -> np.array:
                         ^
SyntaxError: invalid syntax

标签: python-3.xpython-2.7

解决方案


Python 类型提示是在PEP 484中引入的,在 Python 3.5 中实现。因此,它们在 Python 2.x(甚至 Python 3.4)上不可用。

另外,请注意Python 2 EOL - 我建议尽快从 Python 2 过渡。


推荐阅读