[Python-ideas] New 3.x restriction in list comprehensions (original) (raw)

Raymond Hettinger raymond.hettinger at gmail.com
Fri Sep 17 21:44:53 CEST 2010


In Python2, you can transform:

r = [] for x in 2, 4, 6: r.append(x*x+1)

into:

r = [x*x+1 for x in 2, 4, 6]

In Python3, the first still works but the second gives a SyntaxError. It wants the 2, 4, 6 to have parentheses.

The good parts of the change:

The bad parts:

The last part is the one that seems the most problematic. If you write for-loops day in and day out with the unrestricted syntax, you (or least me) will tend to do the wrong thing when writing a list comprehension. It is a bit jarring to get the SyntaxError when the code looks correct -- it took me a bit of fiddling to figure-out what was going on.

My question for the group is whether it would be a good idea to drop the new restriction.

Raymond



More information about the Python-ideas mailing list