Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46dfa5f

Browse files
author
Skip Montanaro
committed
require list comprehensions to start with a for clause
1 parent 2823f03 commit 46dfa5f

7 files changed

Lines changed: 40 additions & 16 deletions

File tree

Doc/ref/ref5.tex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ \subsection{List displays\label{lists}}
153153

154154
\begin{verbatim}
155155
list_display: "[" [listmaker] "]"
156-
listmaker: expression ( list_iter | ( "," expression)* [","] )
156+
listmaker: expression ( list_for | ( "," expression)* [","] )
157157
list_iter: list_for | list_if
158158
list_for: "for" expression_list "in" testlist [list_iter]
159159
list_if: "if" test [list_iter]
@@ -164,7 +164,8 @@ \subsection{List displays\label{lists}}
164164
When a comma-separated list of expressions is supplied, its elements are
165165
evaluated from left to right and placed into the list object in that
166166
order. When a list comprehension is supplied, it consists of a
167-
single expression followed by one or more "for" or "if" clauses. In this
167+
single expression followed by at least one "for" clause and zero or more
168+
"for" or "if" clauses. In this
168169
case, the elements of the new list are those that would be produced
169170
by considering each of the "for" or "if" clauses a block, nesting from
170171
left to right, and evaluating the expression to produce a list element

Doc/tut/tut.tex

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,10 +1755,15 @@ \subsection{Functional Programming Tools \label{functional}}
17551755

17561756
\subsection{List Comprehensions}
17571757

1758-
List comprehensions provide a concise way to create lists without
1759-
resorting to use of the \function{map()} or \function{filter()}
1760-
functions. The resulting construct tends often to be clearer than use
1761-
of those functions.
1758+
List comprehensions provide a concise way to create lists without resorting
1759+
to use of \function{map()}, \function{filter()} and/or \keyword{lambda}.
1760+
The resulting list definition tends often to be clearer than lists built
1761+
using those constructs. Each list comprehension consists of an expression
1762+
following by a \keyword{for} clause, then zero or more \keyword{for} or
1763+
\keyword{if} clauses. The result will be a list resulting from evaluating
1764+
the expression in the context of the \keyword{for} and \keyword{if} clauses
1765+
which follow it. If the expression would evaluate to a tuple, it must be
1766+
parenthesized.
17621767

17631768
\begin{verbatim}
17641769
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
@@ -1771,6 +1776,17 @@ \subsection{List Comprehensions}
17711776
[12, 18]
17721777
>>> [3*x for x in vec if x < 2]
17731778
[]
1779+
>>> [{x: x**2} for x in vec]
1780+
[{2: 4}, {4: 16}, {6: 36}]
1781+
>>> [[x,x**2] for x in vec]
1782+
[[2, 4], [4, 16], [6, 36]]
1783+
>>> [x, x**2 for x in vec] # error - parens required for tuples
1784+
File "<stdin>", line 1
1785+
[x, x**2 for x in vec]
1786+
^
1787+
SyntaxError: invalid syntax
1788+
>>> [(x, x**2) for x in vec]
1789+
[(2, 4), (4, 16), (6, 36)]
17741790
>>> vec1 = [2, 4, 6]
17751791
>>> vec2 = [4, 3, -9]
17761792
>>> [x*y for x in vec1 for y in vec2]

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ term: factor (('*'|'/'|'%') factor)*
7777
factor: ('+'|'-'|'~') factor | power
7878
power: atom trailer* ('**' factor)*
7979
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
80-
listmaker: test ( list_iter | (',' test)* [','] )
80+
listmaker: test ( list_for | (',' test)* [','] )
8181
lambdef: 'lambda' [varargslist] ':' test
8282
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
8383
subscriptlist: subscript (',' subscript)* [',']

Lib/test/output/test_grammar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ classdef
5555
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]
5656
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]
5757
good: got a SyntaxError as expected
58+
good: got a SyntaxError as expected
5859
[('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]

Lib/test/test_grammar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ def meth3(self, a1, a2): pass
578578
except SyntaxError:
579579
print "good: got a SyntaxError as expected"
580580

581+
try:
582+
eval("[x if y]")
583+
print "FAIL: should have raised a SyntaxError!"
584+
except SyntaxError:
585+
print "good: got a SyntaxError as expected"
586+
581587
suppliers = [
582588
(1, "Boeing"),
583589
(2, "Ford"),

Python/compile.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ com_list_iter(struct compiling *c,
10431043
static void
10441044
com_list_comprehension(struct compiling *c, node *n)
10451045
{
1046-
/* listmaker: test list_iter */
1046+
/* listmaker: test list_for */
10471047
char tmpname[12];
10481048
sprintf(tmpname, "__%d__", ++c->c_tmpname);
10491049
com_addoparg(c, BUILD_LIST, 0);
@@ -1052,16 +1052,16 @@ com_list_comprehension(struct compiling *c, node *n)
10521052
com_addopnamestr(c, LOAD_ATTR, "append");
10531053
com_addopnamestr(c, STORE_NAME, tmpname);
10541054
com_pop(c, 1);
1055-
com_list_iter(c, n, CHILD(n, 0), tmpname);
1055+
com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
10561056
com_addopnamestr(c, DELETE_NAME, tmpname);
10571057
--c->c_tmpname;
10581058
}
10591059

10601060
static void
10611061
com_listmaker(struct compiling *c, node *n)
10621062
{
1063-
/* listmaker: test ( list_iter | (',' test)* [','] ) */
1064-
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_iter)
1063+
/* listmaker: test ( list_for | (',' test)* [','] ) */
1064+
if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
10651065
com_list_comprehension(c, n);
10661066
else {
10671067
int len = 0;

Python/graminit.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ static state states_59[4] = {
13171317
{1, arcs_59_3},
13181318
};
13191319
static arc arcs_60_0[2] = {
1320-
{128, 1},
1320+
{120, 1},
13211321
{129, 1},
13221322
};
13231323
static arc arcs_60_1[1] = {
@@ -1340,7 +1340,7 @@ static arc arcs_61_3[1] = {
13401340
{9, 4},
13411341
};
13421342
static arc arcs_61_4[2] = {
1343-
{120, 5},
1343+
{128, 5},
13441344
{0, 4},
13451345
};
13461346
static arc arcs_61_5[1] = {
@@ -1361,7 +1361,7 @@ static arc arcs_62_1[1] = {
13611361
{21, 2},
13621362
};
13631363
static arc arcs_62_2[2] = {
1364-
{120, 3},
1364+
{128, 3},
13651365
{0, 2},
13661366
};
13671367
static arc arcs_62_3[1] = {
@@ -1622,15 +1622,15 @@ static label labels[130] = {
16221622
{25, 0},
16231623
{2, 0},
16241624
{3, 0},
1625-
{316, 0},
1625+
{317, 0},
16261626
{1, "lambda"},
16271627
{314, 0},
16281628
{307, 0},
16291629
{308, 0},
16301630
{309, 0},
16311631
{1, "class"},
16321632
{315, 0},
1633-
{317, 0},
1633+
{316, 0},
16341634
{318, 0},
16351635
};
16361636
grammar _PyParser_Grammar = {

0 commit comments

Comments
 (0)