-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-35224: Add PEP 572 (assignment expressions) to What's New #12941
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
bpo-35224: Add PEP 572 (assignment expressions) to What's New #12941
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be great to have this entry since the last alpha for 3.8 is by Monday.
Personal preference : It would be good to have an example with less nesting of parens.
if name := person.get("name"):
print(f"Hello, {name}")
I'll try to land it ASAP.
Hm, I prefer to use parentheses. In practice most examples need them anyway. Also we heard on the bpo issue that people try |
Makes sense. Thanks 👍 |
There is new syntax (the "walrus operator", ``:=``) to assign values | ||
to variables as part of an expression. Example:: | ||
|
||
if (n := len(a)) > 10: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the recommended way of writing such code? Is it more preferable than a separate statement n = len(a)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing it this way indicates to the human reader (though not to the interpreter -- see the PEP's fine print about scopes :-) that n
is only relevant inside the if
block.
You are really free to write it either way, there is no recommendation intended -- this is just a quick example. (I strongly advise everyone to try and c 8000 ome up with examples that are meaningful, rather than using foo, bar, spam and ham. This one was a compromise. Perhaps someone can come up with a more realistic example where data over (or under!) a certain length must be rejected.
I'm landing this now. @emilyemorehouse please forgive me. See you at PyCon! |
@gvanrossum: Please replace |
This is a stub, others should elaborate. @emilyemorehouse
https://bugs.python.org/issue35224