Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:函数参数类型设置返回syntaxerror_Python_Python 2.7_Function_Types_Arguments - Fatal编程技术网

Python:函数参数类型设置返回syntaxerror

Python:函数参数类型设置返回syntaxerror,python,python-2.7,function,types,arguments,Python,Python 2.7,Function,Types,Arguments,我有一个python脚本,其中包含函数参数的类型声明,如下所示: def dump_var(v: Variable, name: str = None): 据我所知,这是一种有效的语法,用于设置函数的输入参数类型,但它返回 SyntaxError: invalid syntax 可能有什么问题?简短回答:语法错误:无效语法,因为for python2.7类型提示是语法冲突,您可以按照建议在注释中使用python3.5+或使用python2.7类型提示 您可以阅读本文来了解如何在python2

我有一个python脚本,其中包含函数参数的类型声明,如下所示:

def dump_var(v: Variable, name: str = None):
据我所知,这是一种有效的语法,用于设置函数的输入参数类型,但它返回

SyntaxError: invalid syntax

可能有什么问题?

简短回答:语法错误:无效语法,因为for python2.7类型提示是语法冲突,您可以按照建议在注释中使用python3.5+或使用python2.7类型提示

您可以阅读本文来了解如何在python2.7中进行类型提示

某些工具可能希望在必须修改的代码中支持类型注释 与Python 2.7兼容。为此,本政治公众人物建议 (但不是强制性的)函数注释所在的扩展 a#类型:注释。此类评论必须紧跟其后 函数头(在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>
def贪污(self,账户:str,资金:int=1000000,*假收据:str)->无:
“”“使用假收据从账户中挪用资金。”“”

相当于下面的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>
def贪污(自我、账户、资金=1000000,*假收据):
#类型:(str,int,*str)->无
“”“使用假收据从账户中挪用资金。”“”


Python 2.7不支持类型提示,因此这是无效语法。Python 2.7不支持类型提示。