[issue2529] list/generator comprehension parser doesn't match spec

Robert Lehmann report at bugs.python.org
Tue Apr 1 22:04:44 CEST 2008


Robert Lehmann <lehmannro at gmail.com> added the comment:

Your example is parsed as [e for i in (j in ['a','b','c'])] and since
`j` is not defined, you get a NameError. If it was defined, you would
still be iterating a boolean (which is not defined).

Grammatically, this is the following (just the important parts, again):
list_comprehension ::=  expression list_for
list_for ::=  "for" target_list "in" old_expression_list
old_expression_list ::= old_expression
old_expression ::= <stripped test hierarchy...> comparison
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "in"

So your basic misconception is that both `in` keywords are belonging to
the list comprehension syntax -- the former does while the latter is
simply an operator.

----------
nosy: +lehmannro

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2529>
__________________________________


More information about the Python-bugs-list mailing list