why syntax change in lambda

N

Neal Becker

In py2.7 this was accepted, but not in py3.3. Is this intentional? It seems to
violate the 'principle' that extraneous parentheses are usually allowed/ignored

In [1]: p = lambda x: x

In [2]: p = lambda (x): x
File "<ipython-input-2-2b94675a98f1>", line 1
p = lambda (x): x
^
SyntaxError: invalid syntax
 
S

Steven D'Aprano

In py2.7 this was accepted, but not in py3.3. Is this intentional? It
seems to violate the 'principle' that extraneous parentheses are usually
allowed/ignored

In [1]: p = lambda x: x

In [2]: p = lambda (x): x
File "<ipython-input-2-2b94675a98f1>", line 1
p = lambda (x): x
^
SyntaxError: invalid syntax


It is not specific to lambda, it has to do with the removal of argument
unpacking in function argument lists:

# allowed in Python 2, not in Python 3
def f(a, b, (c, d), e):
pass


In Python 3, the parser appears to disallow any extra parentheses inside
the parameter list, even if strictly speaking they don't do anything:

py> def f(a, b, (c), d, e):
File "<stdin>", line 1
def f(a, b, (c), d, e):
^
SyntaxError: invalid syntax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top