forked from python/peps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep-0379.txt
196 lines (136 loc) · 4.92 KB
/
pep-0379.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
PEP: 379
Title: Adding an Assignment Expression
Version: $Revision$
Last-Modified: $Date$
Author: Jervis Whitley <[email protected]>
Status: Withdrawn
Type: Standards Track
Content-Type: text/x-rst
Created: 14-Mar-2009
Python-Version: 2.7, 3.2
Post-History:
Abstract
========
This PEP adds a new assignment expression to the Python language
to make it possible to assign the result of an expression in
almost any place. The new expression will allow the assignment of
the result of an expression at first use (in a comparison for
example).
Motivation and Summary
======================
Issue1714448 "if something as x:" [1]_ describes a feature to allow
assignment of the result of an expression in an if statement to a
name. It supposed that the ``as`` syntax could be borrowed for this
purpose. Many times it is not the expression itself that is
interesting, rather one of the terms that make up the
expression. To be clear, something like this::
if (f_result() == [1, 2, 3]) as res:
seems awfully limited, when this::
if (f_result() as res) == [1, 2, 3]:
is probably the desired result.
Use Cases
=========
See the Examples section near the end.
Specification
=============
A new expression is proposed with the (nominal) syntax::
EXPR -> VAR
This single expression does the following:
- Evaluate the value of ``EXPR``, an arbitrary expression;
- Assign the result to ``VAR``, a single assignment target; and
- Leave the result of ``EXPR`` on the Top of Stack (TOS)
Here ``->`` or (``RARROW``) has been used to illustrate the concept that
the result of ``EXPR`` is assigned to ``VAR``.
The translation of the proposed syntax is::
VAR = (EXPR)
(EXPR)
The assignment target can be either an attribute, a subscript or
name::
f() -> name[0] # where 'name' exists previously.
f() -> name.attr # again 'name' exists prior to this expression.
f() -> name
This expression should be available anywhere that an expression is
currently accepted.
All exceptions that are currently raised during invalid
assignments will continue to be raised when using the assignment
expression. For example, a ``NameError`` will be raised when in
example 1 and 2 above if ``name`` is not previously defined, or an
``IndexError`` if index 0 was out of range.
Examples from the Standard Library
==================================
The following two examples were chosen after a brief search
through the standard library, specifically both are from ast.py
which happened to be open at the time of the search.
Original::
def walk(node):
from collections import deque
todo = deque([node])
while todo:
node = todo.popleft()
todo.extend(iter_child_nodes(node))
yield node
Using assignment expression::
def walk(node):
from collections import deque
todo = deque([node])
while todo:
todo.extend(iter_child_nodes(todo.popleft() -> node))
yield node
Original::
def get_docstring(node, clean=True):
if not isinstance(node, (FunctionDef, ClassDef, Module)):
raise TypeError("%r can't have docstrings"
% node.__class__.__name__)
if node.body and isinstance(node.body[0], Expr) and \
isinstance(node.body[0].value, Str):
if clean:
import inspect
return inspect.cleandoc(node.body[0].value.s)
return node.body[0].value.s
Using assignment expression::
def get_docstring(node, clean=True):
if not isinstance(node, (FunctionDef, ClassDef, Module)):
raise TypeError("%r can't have docstrings"
% node.__class__.__name__)
if node.body -> body and isinstance(body[0] -> elem, Expr) and \
isinstance(elem.value -> value, Str):
if clean:
import inspect
return inspect.cleandoc(value.s)
return value.s
Examples
========
The examples shown below highlight some of the desirable features
of the assignment expression, and some of the possible corner
cases.
1. Assignment in an if statement for use later::
def expensive():
import time; time.sleep(1)
return 'spam'
if expensive() -> res in ('spam', 'eggs'):
dosomething(res)
2. Assignment in a while loop clause::
while len(expensive() -> res) == 4:
dosomething(res)
3. Keep the iterator object from the for loop::
for ch in expensive() -> res:
sell_on_internet(res)
4. Corner case::
for ch -> please_dont in expensive():
pass
# who would want to do this? Not I.
References
==========
.. [1] Issue1714448 "if something as x:", k0wax
http://bugs.python.org/issue1714448
Copyright
=========
This document has been placed in the public domain.
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
coding: utf-8
End: