Skip to content

Commit 1cba25d

Browse files
authored
PEP 671: Add section on evaluation order (#2652)
* PEP 671: Add section on evaluation order This has been a point of some confusion, so I'm just writing in some defaults explicitly. * PEP 671: Fix backticks
1 parent 19f2582 commit 1cba25d

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

pep-0671.rst

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,41 @@ allows the expression to refer to other arguments.
7272

7373
Multiple late-bound arguments are evaluated from left to right, and can refer
7474
to previously-defined values. Order is defined by the function, regardless of
75-
the order in which keyword arguments may be passed. Using names of later
76-
arguments should not be relied upon, and while this MAY work in some Python
77-
implementations, it should be considered dubious::
75+
the order in which keyword arguments may be passed.
7876

7977
def prevref(word="foo", a=>len(word), b=>a//2): # Valid
80-
def selfref(spam=>spam): # Highly likely to give an error
81-
def spaminate(sausage=>eggs + 1, eggs=>sausage - 1): # Confusing, may fail
82-
def frob(n=>len(items), items=[]): # May fail, may succeed
78+
def selfref(spam=>spam): # UnboundLocalError
79+
def spaminate(sausage=>eggs + 1, eggs=>sausage - 1): # Confusing, don't do this
80+
def frob(n=>len(items), items=[]): # See below
8381

84-
Moreover, even if syntactically and semantically legal, this kind of construct
85-
is highly confusing to other programmers, and should be avoided.
82+
Evaluation order is left-to-right; however, implementations MAY choose to do so
83+
in two separate passes, first for all passed arguments and early-bound defaults,
84+
and then a second pass for late-bound defaults. Otherwise, all arguments will be
85+
assigned strictly left-to-right.
8686

87-
88-
Choice of spelling
89-
------------------
87+
Rejected choices of spelling
88+
----------------------------
9089

9190
While this document specifies a single syntax ``name=>expression``, alternate
92-
spellings are similarly plausible. Open for consideration are the following::
91+
spellings are similarly plausible. The following spellings were considered::
9392

9493
def bisect(a, hi=>len(a)):
9594
def bisect(a, hi:=len(a)):
9695
def bisect(a, hi?=len(a)):
97-
98-
An alternative reference implementation is under consideration, which would
99-
use this syntax::
100-
10196
def bisect(a, @hi=len(a)):
10297

10398
Since default arguments behave largely the same whether they're early or late
104-
bound, the syntax is deliberately similar to the existing early-bind syntax.
99+
bound, the chosen syntax ``hi=>len(a)`` is deliberately similar to the existing
100+
early-bind syntax.
101+
102+
One reason for rejection of the ``:=`` syntax is its behaviour with annotations.
103+
Annotations go before the default, so in all syntax options, it must be
104+
unambiguous (both to the human and the parser) whether this is an annotation,
105+
a default, or both. The alternate syntax ``target:=expr`` runs the risk of
106+
being misinterpreted as ``target:int=expr`` with the annotation omitted in
107+
error, and may thus mask bugs. The chosen syntax ``target=>expr`` does not
108+
have this problem.
109+
105110

106111
How to Teach This
107112
=================
@@ -137,17 +142,6 @@ be defined by the function. Additionally, dedicated sentinel objects can be
137142
used as dictionary lookup keys, where :pep:`671` does not apply.
138143

139144

140-
Interaction with annotations
141-
============================
142-
143-
Annotations go before the default, so in all syntax options, it must be
144-
unambiguous (both to the human and the parser) whether this is an annotation,
145-
a default, or both. The alternate syntax ``target:=expr`` runs the risk of
146-
being misinterpreted as ``target:int=expr`` with the annotation omitted in
147-
error, and may thus mask bugs. The preferred syntax ``target=>expr`` does not
148-
have this problem.
149-
150-
151145
Implementation details
152146
======================
153147

@@ -210,14 +204,3 @@ Copyright
210204

211205
This document is placed in the public domain or under the
212206
CC0-1.0-Universal license, whichever is more permissive.
213-
214-
215-
216-
..
217-
Local Variables:
218-
mode: indented-text
219-
indent-tabs-mode: nil
220-
sentence-end-double-space: t
221-
fill-column: 70
222-
coding: utf-8
223-
End:

0 commit comments

Comments
 (0)