[Python-Dev] list comprehensions again...
Tue, 11 Jul 2000 16:04:06 +0200
On Tue, Jul 11, 2000 at 09:40:32AM -0500, Guido van Rossum wrote:
> > [x,y for x in (1,2,3) for y in (4,5,6)]
> Hm, I suppose the programmer meant the same as
> [(x,y) for x in (1,2,3) for y in (4,5,6)]
> Would it be really bad if we forced them to write it that way?
Actually, the change in Grammar wouldn't enforce that. (At least, I think
not.) Because the 'for' splits and disambiguates before the ',' in the first
tuple, the parser will always see it as such, and it's probably pretty hard
to convince the parser not to :-)
Because of the way 'testlist' and 'test' are defined, it's easy to use them
interchangeably. I think, anyway. I'm still new at this parsing thing :-)
But the Gramar uses 'testlist' to mean the contents of the list itself, so
it seems logical to me that this should be the case wether or not 'testlist'
is followed by 'list_iter' -- currently, the presense of list_iter changes
(the interpretation of) it from the contents of a list to a tuple.
Like I said, it was nit-picky ;)
> Side note: the suggested syntax here (for i in seq1 for j in seq2)
> looks like a potential alternative for the currently proposed parallel
> for loop syntax (for i in seq1; j in seq2).
> Only problem: the for-for variant could be mistaken by someone used to
> see nested loops as meaning the same as
> for i in seq1:
> for j in seq2: ...
I personally prefer 'for i in seq1; j in seq2' because it's a lot less
ambiguous. More importantly, however, the meaning in the cited
list-comprehension example is different !
[x,y for x in (1,2,3) for y in (4,5,6)]
is actually turned into a pair of nested loops:
tmp = 
for x in (1,2,3):
for y in (4,5,6):
(Quite literally so, in fact ;) So it's *not* the same as the
tmp = 
for x in (1,2,3); y in (4,5,6):
For that reason alone, I'd go for the 2nd syntax ;) Unless, of course, you
have a proposed new syntax for the nested-for-loop in list comprehensions
> Undecided and unhelpful,
Thomas Wouters <email@example.com>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!