Update PEP-572 for f-strings (see bpo-36798) (#1024)

See bpo-36798.

This update specifies the interplay between f-strings and
the assignment operator. Specifically, it attempts to
clarify how to use assignment operators inside of f-strings.
This commit is contained in:
Logan Jones 2019-05-06 12:30:23 -04:00 committed by Guido van Rossum
parent ad68d00ba1
commit 6a705de78e
1 changed files with 14 additions and 0 deletions

View File

@ -208,6 +208,20 @@ in order to avoid ambiguities or user confusion:
used more than once, the expression is likely to need parenthesizing anyway, used more than once, the expression is likely to need parenthesizing anyway,
so this prohibition will rarely affect code. so this prohibition will rarely affect code.
- Assignment expressions inside of f-strings require parentheses. Example::
>>> f'{(x:=10)}' # Valid, uses assignment expression
'10'
>>> x = 10
>>> f'{x:=10}' # Valid, passes '=10' to formatter
' 10'
This shows that what looks like an assignment operator in an f-string is
not always an assignment operator. The f-string parser uses ``:`` to
indicate formatting options. To preserve backwards compatibility,
assignment operator usage inside of f-strings must be parenthesized.
As noted above, this usage of the assignment operator is not recommended.
Scope of the target Scope of the target
------------------- -------------------