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

Question

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

Answers 2
Added at 2017-10-07 04:10
Tags
Question

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?

Answers to

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

nr: #1 dodano: 2017-10-07 04:10

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
    ...
nr: #2 dodano: 2017-10-07 04:10

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.

Source Show
◀ Wstecz