Skip to content

PEP 572: Priority relative to lambda is awkward #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gvanrossum opened this issue Jul 10, 2018 · 3 comments
Closed

PEP 572: Priority relative to lambda is awkward #728

gvanrossum opened this issue Jul 10, 2018 · 3 comments
Assignees

Comments

@gvanrossum
Copy link
Member

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)

But this would imply a perverse priority relationship between := and lambda: when := is to the left of lambda, the lambda binds tighter (the grouping is the same as f := (lambda: 42)), while when := is to the right, the := binds tifhter (the grouping is lambda: (f := 42)).

IIRC there's always something funky with unary operators. Perhaps we should at least have a paragraph about this in the PEP showing these that these two examples should both be valid? Are there awkward consequences of ruling both valid?

@Rosuav
Copy link
Contributor

Rosuav commented Jul 10, 2018

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.

@gvanrossum
Copy link
Member Author

Oh you're right! We don't want to do the same thing as within comprehensions, and as soon as you're going to use the bound name elsewhere in the lambda you'll need parentheses anyway, e.g.

lambda line: (m := re.match(pattern, line)) and m.group(1)

Can you come up with a PR to highlight these details? I guess we can describe := as binding less tight than lambda and add a note explaining that this implies := inside a lambda must be parenthesized, and showing the two examples?

@Rosuav
Copy link
Contributor

Rosuav commented Jul 10, 2018

Migrating to #729

@Rosuav Rosuav closed this as completed Jul 10, 2018
gvanrossum pushed a commit that referenced this issue Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants