2017-01-30 43 views
1

我有一个包含函数参数类型声明如下Python脚本:的Python:函数参数类型,设置在返回的SyntaxError

def dump_var(v: Variable, name: str = None): 

据我所知,这是一个有效的语法,设置的输入类型参数给功能,但它返回一个

SyntaxError: invalid syntax 

什么可能是错误的?

+2

的Python 2.7不支持类型提示,所以这是无效的语法。 – kindall

+0

Python 2.7不支持类型提示。 – Blender

回答

2

答案很简单:语法错误:无效的语法,因为对于python2.7类型暗示是一个语法错误,可以选择使用python3或使用类型提示在评论python2.7PEP建议

您可以阅读本文,以了解如何在python2.7中使用类型提示Suggested syntax for python2.7 and straddling clode

Some tools may want to support type annotations in code that must be compatible with Python 2.7. For this purpose this PEP has a suggested (but not mandatory) extension where function annotations are placed in a # type: comment. Such a comment must be placed immediately following the function header (before the docstring)

一个例子:

以下Python代码3:

def embezzle(self, account: str, funds: int = 1000000, *fake_receipts: str) -> None: 
    """Embezzle funds from account using fake receipts.""" 
    <code goes here> 

等效于以下Python代码2.7

def embezzle(self, account, funds=1000000, *fake_receipts): 
    # type: (str, int, *str) -> None 
    """Embezzle funds from account using fake receipts.""" 
    <code goes here>