@@ -72,36 +72,41 @@ allows the expression to refer to other arguments.
72
72
73
73
Multiple late-bound arguments are evaluated from left to right, and can refer
74
74
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.
78
76
79
77
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
83
81
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.
86
86
87
-
88
- Choice of spelling
89
- ------------------
87
+ Rejected choices of spelling
88
+ ----------------------------
90
89
91
90
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 ::
93
92
94
93
def bisect(a, hi=>len(a)):
95
94
def bisect(a, hi:=len(a)):
96
95
def bisect(a, hi?=len(a)):
97
-
98
- An alternative reference implementation is under consideration, which would
99
- use this syntax::
100
-
101
96
def bisect(a, @hi=len(a)):
102
97
103
98
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
+
105
110
106
111
How to Teach This
107
112
=================
@@ -137,17 +142,6 @@ be defined by the function. Additionally, dedicated sentinel objects can be
137
142
used as dictionary lookup keys, where :pep: `671 ` does not apply.
138
143
139
144
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
-
151
145
Implementation details
152
146
======================
153
147
@@ -210,14 +204,3 @@ Copyright
210
204
211
205
This document is placed in the public domain or under the
212
206
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