摆脱SyntaxError:Python中无效的语法

时间:2019-01-08 23:34:15

标签: python python-2.7

def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
E                               ^
E   SyntaxError: invalid syntax

不知道这里发生了什么。

代码:

 def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
        input1 = str(input1)
        input2 = str(input2)
        input3 = str(input3)
        print(Color.BOLD + Color.GREEN + " " + Color.CHECKMARK + " " + input1 + Color.END + " " + input2 + " " + input3)

2 个答案:

答案 0 :(得分:4)

由于您使用的是Python 2.7,因此需要备份到该语法。类型提示是Python 3;删除它们。

def success(self, input1="", input2="", input3=""):

答案 1 :(得分:2)

您发布的函数是有效的Python 3.7。类型提示为added to Python in version 3.5.0,您发布的代码应适用于3.5.0以后的任何版本:

Python 3.7.0 (default, Aug 22 2018, 20:50:05) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
...         input1 = str(input1)
...         input2 = str(input2)
...         input3 = str(input3)
...         print(Color.BOLD + Color.GREEN + " " + Color.CHECKMARK + " " + input1 + 
Color.END + " " + input2 + " " + input3)
... 
>>> success
<function success at 0x7fb70e4c61e0>

我怀疑您使用的是不支持这些语言功能的旧python版本。