8000 bpo-35224: Add PEP 572 (assignment expressions) to What's New by gvanrossum · Pull Request #12941 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div id="repo-content-pjax-container" class="repository-content " >

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Issue 35224: Add PEP 572 (assignment expressions) to What's New
  • Loading branch information
gvanrossum committed Apr 24, 2019
commit 9f0bad762b7d334dfdedd5ccce434e3b5da77c00
16 changes: 16 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ Summary -- Release highlights
New Features
============

Assignment expressions
----------------------

There is new syntax (the "walrus operator", ``:=``) to assign values
to variables as part of an expression. Example::

if (n := len(a)) > 10:
Copy link
Member

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)?

Copy link
Member Author

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 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.

print(f"List is too long ({n} elements, expected <= 10)")

See :pep:`572` for a full description.

(Contributed by Emily Morehouse in :issue:`35224`.)

.. TODO: Emily will sprint on docs at PyCon US 2019.


Parallel filesystem cache for compiled bytecode files
-----------------------------------------------------

Expand Down
0