[英]python 2 syntax error when running python 3 code
我有一堂课,看起来像下面这样
class ExperimentResult(BaseDataObject):
def __init__(self, result_type: str, data: dict, references: list):
super().__init__()
self.type = result_type
self.references = references
self.data = data
def __repr__(self):
return str(self.__dict__)
我尝试在python 2中运行时,代码是用python 3编写的。运行时,我得到了
def __init__(self, result_type: str, data: dict, references: list):
^
SyntaxError: invalid syntax
是否有一个“ import_from_future”来解决这个问题?
不,没有__future__
开关可以在Python 2中启用Python 3注释。如果将注释用于类型提示,请改用注释。
有关语法的详细信息,请参见PEP 484的“ Python 2.7和跨代码示例”和“ 类型检查Python 2代码”部分 。
对于需要与Python 2.7兼容的代码,由于在Python 3中引入了函数注释语法,因此注释中给出了函数类型注释。
对于您的特定示例,应为:
class ExperimentResult(BaseDataObject):
def __init__(self, result_type, data, references):
# type: (str, dict, list) -> None
super().__init__()
self.type = result_type
self.references = references
self.data = data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.