[Python-bugs-list] [ python-Bugs-431886 ] listcomp syntax too confusing (tuples) (original) (raw)

noreply@sourceforge.net noreply@sourceforge.net
Mon, 15 Oct 2001 08:30:52 -0700


Bugs item #431886, was opened at 2001-06-08 23:34 You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=431886&group_id=5470

Category: Parser/Compiler Group: None Status: Open Resolution: None Priority: 7 Submitted By: Tim Peters (tim_one) Assigned to: Guido van Rossum (gvanrossum) Summary: listcomp syntax too confusing (tuples)

Initial Comment: We were careful to make sure that tuple targets in listcomps required parens, i.e.

[x, x+1 for x in s] # rejected [(x, x+1) for x in s] # OK

but we didn't anticipate other "surprise tuple" cases. Most recently from c.l.py,

""" I tried the one-line command in a interaction mode: [x for x in [1, 2, 3], y for y in [4, 5, 6]] and the result surprised me, that is: [[1,2,3],[1,2,3],[1,2,3],9,9,9] Who can explain the behavior? Since I expected the result should be: [[1,4],[1,5],[1,6],[2,4],...] """

This is too surprising; we should require that the listcomp be spelled

[x for x in ([1, 2, 3], y) for y in [4, 5, 6]]

instead if that's really what they want (which it almost certainly isn't!).


Comment By: Guido van Rossum (gvanrossum) Date: 2001-10-15 08:30

Message: Logged In: YES user_id=6380

I'll fix it in a way that isn't quite so drastic -- you'll be able to have a testlist there and even one that ends in a comma, except that a singleton list cannot end in a comma.

Expect checkins soon.


Comment By: Tim Peters (tim_one) Date: 2001-10-14 15:27

Message: Logged In: YES user_id=31435

Guido, this is easy to fix:

diff -u -r1.44 Grammar --- Grammar 2001/08/08 05:00:17 1.44 +++ Grammar 2001/10/14 21:57:12 @@ -97,5 +97,5 @@ argument: [test '='] test # Really [keyword '='] test

list_iter: list_for | list_if -list_for: 'for' exprlist 'in' testlist [list_iter] +list_for: 'for' exprlist 'in' test [list_iter] list_if: 'if' test [list_iter]

test_parser fails then, and parsermodule.c also needs a patch. A combined context diff is attached.

I'm assigning this to you because it's not wholly backward compatible; e.g.,

[i**2 for i in 1, 2]

is no longer accepted -- but then that's the point of the exercise. I don't know of any real code that breaks. Risk it? Forget it? PEP? YAFS (yet another future stmt)?

Bumped the priority, because if we are going to change it, we should do so for 2.2.


You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=431886&group_id=5470