bpo-35224: Add PEP 572 (assignment expressions) to What's New#12941
bpo-35224: Add PEP 572 (assignment expressions) to What's New#12941gvanrossum merged 1 commit intopython:masterfrom
Conversation
tirkarthi
left a comment
There was a problem hiding this comment.
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.
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.
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 come 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