Python SyntaxError:无效的语法,在python3中的函数参数允许括号?
问题描述:
该功能可在python2:Python SyntaxError:无效的语法,在python3中的函数参数允许括号?
def setCellValue(self, (x, y), value):
self.map[x][y] = value
但是,当我试图在python3它显示无效的语法:
def setCellValue(self, (x, y), value):
^
SyntaxError: invalid syntax
是它的支架问题?我怎样才能解决这个py3?
答
是的,元组解包在python3中被移除。据PEP 3113:
The example function at the beginning of this PEP could easily be rewritten as:
def fxn(a, b_c, d): b, c = b_c pass
and in no way lose functionality.
它的存在只起到了语法和字节码生成复杂,所以它已被删除。
答
是的,该功能已在Python 3中取出。它在签名检查中表现不佳。你必须自己解开这个论点:
def setCellValue(self, pos, value):
x, y = pos
...