The function works in python2:
def setCellValue(self, (x, y), value):
self.map[x][y] = value
But when I'm trying in python3 it shows invalid syntax:
def setCellValue(self, (x, y), value):
^
SyntaxError: invalid syntax
Is it the bracket problem? How can I fix this in py3?
You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.
Some of the most common causes of syntax errors in Python are: Misspelled reserved keywords. Missing quotes. Missing required spaces.
Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.
How to Fix It: If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.
Yes, tuple unpacking was removed in python3. According to 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.
It's existence served only to complicate the grammar and bytecode generation, so it was removed.
Yeah, that feature got taken out in Python 3. It didn't play well with signature inspection. You have to unpack the argument yourself:
def setCellValue(self, pos, value):
x, y = pos
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With