Skip to content
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
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
Copy Markdown
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
Copy Markdown
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