Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
46dfa5f4
Commit
46dfa5f4
authored
Aug 22, 2000
by
Skip Montanaro
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
require list comprehensions to start with a for clause
parent
2823f03a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
40 additions
and
16 deletions
+40
-16
Doc/ref/ref5.tex
Doc/ref/ref5.tex
+3
-2
Doc/tut/tut.tex
Doc/tut/tut.tex
+20
-4
Grammar/Grammar
Grammar/Grammar
+1
-1
Lib/test/output/test_grammar
Lib/test/output/test_grammar
+1
-0
Lib/test/test_grammar.py
Lib/test/test_grammar.py
+6
-0
Python/compile.c
Python/compile.c
+4
-4
Python/graminit.c
Python/graminit.c
+5
-5
No files found.
Doc/ref/ref5.tex
View file @
46dfa5f4
...
...
@@ -153,7 +153,7 @@ square brackets:
\begin{verbatim}
list
_
display: "[" [listmaker] "]"
listmaker: expression ( list
_
ite
r | ( "," expression)* [","] )
listmaker: expression ( list
_
fo
r | ( "," expression)* [","] )
list
_
iter: list
_
for | list
_
if
list
_
for: "for" expression
_
list "in" testlist [list
_
iter]
list
_
if: "if" test [list
_
iter]
...
...
@@ -164,7 +164,8 @@ by providing either a list of expressions or a list comprehension.
When a comma-separated list of expressions is supplied, its elements are
evaluated from left to right and placed into the list object in that
order. When a list comprehension is supplied, it consists of a
single expression followed by one or more "for" or "if" clauses. In this
single expression followed by at least one "for" clause and zero or more
"for" or "if" clauses. In this
case, the elements of the new list are those that would be produced
by considering each of the "for" or "if" clauses a block, nesting from
left to right, and evaluating the expression to produce a list element
...
...
Doc/tut/tut.tex
View file @
46dfa5f4
...
...
@@ -1755,10 +1755,15 @@ item, then to the result and the next item, and so on. For example,
\subsection
{
List Comprehensions
}
List comprehensions provide a concise way to create lists without
resorting to use of the
\function
{
map()
}
or
\function
{
filter()
}
functions. The resulting construct tends often to be clearer than use
of those functions.
List comprehensions provide a concise way to create lists without resorting
to use of
\function
{
map()
}
,
\function
{
filter()
}
and/or
\keyword
{
lambda
}
.
The resulting list definition tends often to be clearer than lists built
using those constructs. Each list comprehension consists of an expression
following by a
\keyword
{
for
}
clause, then zero or more
\keyword
{
for
}
or
\keyword
{
if
}
clauses. The result will be a list resulting from evaluating
the expression in the context of the
\keyword
{
for
}
and
\keyword
{
if
}
clauses
which follow it. If the expression would evaluate to a tuple, it must be
parenthesized.
\begin{verbatim}
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
...
...
@@ -1771,6 +1776,17 @@ of those functions.
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
>>> [
{
x: x**2
}
for x in vec]
[
{
2: 4
}
,
{
4: 16
}
,
{
6: 36
}
]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec] # error - parens required for tuples
File "<stdin>", line 1
[x, x**2 for x in vec]
^
SyntaxError: invalid syntax
>>> [(x, x**2) for x in vec]
[(2, 4), (4, 16), (6, 36)]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x*y for x in vec1 for y in vec2]
...
...
Grammar/Grammar
View file @
46dfa5f4
...
...
@@ -77,7 +77,7 @@ term: factor (('*'|'/'|'%') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom trailer* ('**' factor)*
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
listmaker: test ( list_
ite
r | (',' test)* [','] )
listmaker: test ( list_
fo
r | (',' test)* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
...
...
Lib/test/output/test_grammar
View file @
46dfa5f4
...
...
@@ -55,4 +55,5 @@ classdef
[(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')]
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]
good: got a SyntaxError as expected
good: got a SyntaxError as expected
[('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]
Lib/test/test_grammar.py
View file @
46dfa5f4
...
...
@@ -578,6 +578,12 @@ try:
except
SyntaxError
:
print
"good: got a SyntaxError as expected"
try
:
eval
(
"[x if y]"
)
print
"FAIL: should have raised a SyntaxError!"
except
SyntaxError
:
print
"good: got a SyntaxError as expected"
suppliers
=
[
(
1
,
"Boeing"
),
(
2
,
"Ford"
),
...
...
Python/compile.c
View file @
46dfa5f4
...
...
@@ -1043,7 +1043,7 @@ com_list_iter(struct compiling *c,
static
void
com_list_comprehension
(
struct
compiling
*
c
,
node
*
n
)
{
/* listmaker: test list_
ite
r */
/* listmaker: test list_
fo
r */
char
tmpname
[
12
];
sprintf
(
tmpname
,
"__%d__"
,
++
c
->
c_tmpname
);
com_addoparg
(
c
,
BUILD_LIST
,
0
);
...
...
@@ -1052,7 +1052,7 @@ com_list_comprehension(struct compiling *c, node *n)
com_addopnamestr
(
c
,
LOAD_ATTR
,
"append"
);
com_addopnamestr
(
c
,
STORE_NAME
,
tmpname
);
com_pop
(
c
,
1
);
com_list_
iter
(
c
,
n
,
CHILD
(
n
,
0
),
tmpname
);
com_list_
for
(
c
,
CHILD
(
n
,
1
)
,
CHILD
(
n
,
0
),
tmpname
);
com_addopnamestr
(
c
,
DELETE_NAME
,
tmpname
);
--
c
->
c_tmpname
;
}
...
...
@@ -1060,8 +1060,8 @@ com_list_comprehension(struct compiling *c, node *n)
static
void
com_listmaker
(
struct
compiling
*
c
,
node
*
n
)
{
/* listmaker: test ( list_
ite
r | (',' test)* [','] ) */
if
(
NCH
(
n
)
>
1
&&
TYPE
(
CHILD
(
n
,
1
))
==
list_
ite
r
)
/* listmaker: test ( list_
fo
r | (',' test)* [','] ) */
if
(
NCH
(
n
)
>
1
&&
TYPE
(
CHILD
(
n
,
1
))
==
list_
fo
r
)
com_list_comprehension
(
c
,
n
);
else
{
int
len
=
0
;
...
...
Python/graminit.c
View file @
46dfa5f4
...
...
@@ -1317,7 +1317,7 @@ static state states_59[4] = {
{
1
,
arcs_59_3
},
};
static
arc
arcs_60_0
[
2
]
=
{
{
12
8
,
1
},
{
12
0
,
1
},
{
129
,
1
},
};
static
arc
arcs_60_1
[
1
]
=
{
...
...
@@ -1340,7 +1340,7 @@ static arc arcs_61_3[1] = {
{
9
,
4
},
};
static
arc
arcs_61_4
[
2
]
=
{
{
12
0
,
5
},
{
12
8
,
5
},
{
0
,
4
},
};
static
arc
arcs_61_5
[
1
]
=
{
...
...
@@ -1361,7 +1361,7 @@ static arc arcs_62_1[1] = {
{
21
,
2
},
};
static
arc
arcs_62_2
[
2
]
=
{
{
12
0
,
3
},
{
12
8
,
3
},
{
0
,
2
},
};
static
arc
arcs_62_3
[
1
]
=
{
...
...
@@ -1622,7 +1622,7 @@ static label labels[130] = {
{
25
,
0
},
{
2
,
0
},
{
3
,
0
},
{
31
6
,
0
},
{
31
7
,
0
},
{
1
,
"lambda"
},
{
314
,
0
},
{
307
,
0
},
...
...
@@ -1630,7 +1630,7 @@ static label labels[130] = {
{
309
,
0
},
{
1
,
"class"
},
{
315
,
0
},
{
31
7
,
0
},
{
31
6
,
0
},
{
318
,
0
},
};
grammar
_PyParser_Grammar
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment