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:
parent
ad68d00ba1
commit
6a705de78e
14
pep-0572.rst
14
pep-0572.rst
|
@ -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
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue