[Python-ideas] New 3.x restriction in list comprehensions

Dirkjan Ochtman dirkjan at ochtman.nl
Sat Sep 18 00:00:57 CEST 2010


On Fri, Sep 17, 2010 at 21:44, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> My question for the group is whether it would be a good
> idea to drop the new restriction.

I like the restriction and would actually advocate having it for
regular for-loops too (though that would be a big no-no, I guess).

Here's why I never use them without parenthesis, in python 2:

>>> (1 if True else 3, 4)
(1, 4)
>>> (lambda x: x * x, 6)
(<function <lambda> at 0x100475ed8>, 6)
>>> [i for i in 2, 3]
[2, 3]
>>> (i for i in 2, 3)
  File "<stdin>", line 1
    (i for i in 2, 3)
                 ^
SyntaxError: invalid syntax

And in Python 3:

>>> (1 if True else 3, 4)
(1, 4)
>>> (lambda x: x * x, 6)
(<function <lambda> at 0x7f4ef41785a0>, 6)
>>> [i for i in 2, 3]
  File "<stdin>", line 1
    [i for i in 2, 3]
                 ^
SyntaxError: invalid syntax
>>> (i for i in 2, 3)
  File "<stdin>", line 1
    (i for i in 2, 3)
                 ^
SyntaxError: invalid syntax

Cheers,

Dirkjan



More information about the Python-ideas mailing list