PEP 572: Weaken proposal / strengthen requirements from "target" to "name"
This commit is contained in:
parent
0f8a0669b4
commit
327d54250c
27
pep-0572.rst
27
pep-0572.rst
|
@ -35,9 +35,8 @@ Syntax and semantics
|
||||||
====================
|
====================
|
||||||
|
|
||||||
In any context where arbitrary Python expressions can be used, a **named
|
In any context where arbitrary Python expressions can be used, a **named
|
||||||
expression** can appear. This is of the form ``target := expr`` where
|
expression** can appear. This is of the form ``name := expr`` where
|
||||||
``expr`` is any valid Python expression, and ``target`` is any valid
|
``expr`` is any valid Python expression, and ``name`` is an identifier.
|
||||||
assignment target.
|
|
||||||
|
|
||||||
The value of such a named expression is the same as the incorporated
|
The value of such a named expression is the same as the incorporated
|
||||||
expression, with the additional side-effect that the target is assigned
|
expression, with the additional side-effect that the target is assigned
|
||||||
|
@ -70,6 +69,15 @@ and is therefore processed right-to-left, as if it were spelled thus::
|
||||||
|
|
||||||
assert 0 == (x := (y := (z := 0)))
|
assert 0 == (x := (y := (z := 0)))
|
||||||
|
|
||||||
|
Statement assignment can include annotations. This would be syntactically
|
||||||
|
noisy in expressions, and is of minor importance. An annotation can be
|
||||||
|
given separately from the assignment if needed::
|
||||||
|
|
||||||
|
x:str = "" # works
|
||||||
|
(x:str := "") # SyntaxError
|
||||||
|
x:str # possibly before a loop
|
||||||
|
(x := "") # fine
|
||||||
|
|
||||||
Augmented assignment is not supported in expression form::
|
Augmented assignment is not supported in expression form::
|
||||||
|
|
||||||
>>> x +:= 1
|
>>> x +:= 1
|
||||||
|
@ -78,6 +86,10 @@ Augmented assignment is not supported in expression form::
|
||||||
^
|
^
|
||||||
SyntaxError: invalid syntax
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
Statement assignment is able to set attributes and subscripts, but
|
||||||
|
expression assignment is restricted to names. (This restriction may be
|
||||||
|
relaxed in a future version of Python.)
|
||||||
|
|
||||||
Otherwise, the semantics of assignment are identical in statement and
|
Otherwise, the semantics of assignment are identical in statement and
|
||||||
expression forms.
|
expression forms.
|
||||||
|
|
||||||
|
@ -522,8 +534,7 @@ With assignment expressions, why bother with assignment statements?
|
||||||
|
|
||||||
The two forms have different flexibilities. The ``:=`` operator can be used
|
The two forms have different flexibilities. The ``:=`` operator can be used
|
||||||
inside a larger expression; the ``=`` statement can be augmented to ``+=`` and
|
inside a larger expression; the ``=`` statement can be augmented to ``+=`` and
|
||||||
its friends. The assignment statement is a clear declaration of intent: this
|
its friends, can be chained, and can assign to attributes and subscripts.
|
||||||
value is to be assigned to this target, and that's it.
|
|
||||||
|
|
||||||
|
|
||||||
Why not use a sublocal scope and prevent namespace pollution?
|
Why not use a sublocal scope and prevent namespace pollution?
|
||||||
|
@ -550,9 +561,9 @@ suggestions to move the proposal in this direction. [2]_)
|
||||||
Style guide recommendations
|
Style guide recommendations
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
As this adds another way to spell some of the same effects as can already be
|
As expression assignments can be used somewhat equivalently to statement
|
||||||
done, it is worth noting a few broad recommendations. These could be included
|
assignments, the question of which should be preferred will arise. For the
|
||||||
in PEP 8 and/or other style guides.
|
benefit of style guides such as PEP 8, two recommendations are suggested.
|
||||||
|
|
||||||
1. If either assignment statements or assignment expressions can be
|
1. If either assignment statements or assignment expressions can be
|
||||||
used, prefer statements; they are a clear declaration of intent.
|
used, prefer statements; they are a clear declaration of intent.
|
||||||
|
|
Loading…
Reference in New Issue