[Python-Dev] Re: accumulator display syntax
David Eppstein
eppstein at ics.uci.edu
Fri Oct 17 17:14:41 EDT 2003
In article <16272.22369.546606.870697 at montanaro.dyndns.org>,
Skip Montanaro <skip at pobox.com> wrote:
> Parens are required in certain situations within list comprehensions around
> tuples (probably for syntactic reasons, but perhaps to aid the reader as
> well) where tuples can often be defined without enclosing parens. Here's a
> contrived example:
>
> >>> [(a,b) for (a,b) in zip(range(5), range(10))]
> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
> >>> [a,b for (a,b) in zip(range(5), range(10))]
> File "<stdin>", line 1
> [a,b for (a,b) in zip(range(5), range(10))]
> ^
> SyntaxError: invalid syntax
This one has bitten me several times.
When it does, I discover the error quickly due to the syntax error, but
it would be bad if this became valid syntax and returned a list [a,X]
where X is an iterator. I don't think you could count on this getting
caught by a being unbound, because often the variables in list
comprehensions can be single letters that shadow previous bindings.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-Dev
mailing list