On 06/10/2021 23:53, Chris Angelico wrote:
On Thu, Oct 7, 2021 at 8:51 AM Thomas Jollans<t...@tjol.eu>  wrote:
On 03/10/2021 01:39, Chris Angelico wrote:
Using assignment expressions in lambda functions sometimes works, but
sometimes doesn't.
Does this commit by a certain Chris Angelico help clear things up?

https://github.com/python/peps/commit/f906b988b20c9a8e7e13a2262f5381bd2b1399e2
No, because the examples I gave don't fit into that :) I was aware of
what the PEP originally said.

If you try to align the examples with the descriptions in the PEP,
you'll find that they don't all match.

ChrisA


The issue closed by that commit is interesting, if nothing else:

Guido van Rossum wrote:

The PEP says very little about lambda. I feel the following two examples should both be valid:

foo(f := lambda: 42, flag=True)
foo(lambda: f := 42, flag=True)
Chris Angelico wrote:

The second example is kinda bizarre though; it's going to create a fairly useless name binding within the lambda function. (Unless you want to give lambda functions the same magic as comprehensions, making the name f exist in the same scope where foo is being run.) So I would be okay with the first example doing the obvious thing, and the second one requiring parentheses lambda: (f := 42) for syntactic validity.

I think that at least clears up this part:

# But a SyntaxError if parenthesized like this??
def f(n):
...     return (lambda: n := 1)
   File "<stdin>", line 2
     return (lambda: n := 1)
             ^^^^^^^^^
SyntaxError: cannot use assignment expressions with lambda

# Oh, and it doesn't actually assign anything.
def f(n):
...     return (lambda: (n := 1)), (lambda: n)
...


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to