[Python-Dev] Re: accumulator display syntax
Guido van Rossum
guido at python.org
Fri Oct 17 17:48:33 EDT 2003
> > >>> [(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,
Generally, when we talk about something "biting", we mean something
that *doesn't* give a syntax error, but silently does something quite
different than what you'd naively expect.
This was made a syntax error specifically because of this ambiguity.
> 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.
No, [a,X] would be a syntax error if X was an iterator comprehension.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list