Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SyntaxError: invalid syntax, are brackets allowed in function parameters in python3?

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?

like image 875
Cathyyang Avatar asked Oct 07 '17 02:10

Cathyyang


People also ask

How do I fix SyntaxError invalid syntax in Python?

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.

Why does Python keep saying invalid syntax?

Some of the most common causes of syntax errors in Python are: Misspelled reserved keywords. Missing quotes. Missing required spaces.

Which of the following will give a syntax error in Python?

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 do you fix a syntax error?

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.


2 Answers

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.

like image 64
cs95 Avatar answered Nov 09 '22 07:11

cs95


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
    ...
like image 22
user2357112 supports Monica Avatar answered Nov 09 '22 08:11

user2357112 supports Monica