Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-86482: Document assignment expression need for ()s #23291

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 8 commits into from
Oct 6, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Update Doc/reference/expressions.rst
  • Loading branch information
JelleZijlstra authored Oct 3, 2022
commit a9d94bff2202e97c8f3b1b4bd3ac4c7d7e45e1ee
3 changes: 2 additions & 1 deletion 3 Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,8 @@ Or, when processing a file stream in chunks:

Assignment expressions must be surrounded by parentheses when used
as sub-expressions in slicing, conditional, lambda,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unparenthesized assignment expressions in slices were implemented in #23317, so they are allowed since 3.10. Also, assignment expression need to parenthesized in assert and with statements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unparenthesized walruses are allowed in index expressions but not slices. This is current 3.12:

>>> x[y:=3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x[y:=3:z:=4]
  File "<stdin>", line 1
    x[y:=3:z:=4]
          ^
SyntaxError: invalid syntax

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! I mixed up indexes/slices. Sorry!

keyword-argument, and comprehension-if expressions.
keyword-argument, and comprehension-if expressions
and in ``assert`` and ``with`` statements.
In all other places where they can be used, parentheses are not required,
including in ``if`` and ``while`` statements.

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.