Generator expression parenthesis mixed with function call ones

Facundo Batista facundo at taniquetil.com.ar
Wed Mar 7 17:08:44 CET 2007


Laurent Pointal wrote:


>>>> f(4,i for i in range(10))
>   File "<stdin>", line 1
> SyntaxError: invalid syntax
>
>
> Why does Python allow generator expression parenthesis to be mixed with
> function call parenthesis when there is only one parameter ?

For simplicity and elegant coding, so you can do something like you did
at first:

  sum(i for i in range(10))


> IMHO this should be forbidden, usage must not be different when there is
> only one parameter and when there are more parameters.

The problem in your last test is that if you use more than one argument,
you *must* use the parenthesis. In Py2.5 there's a better message error:

>>> f(4,i for i in range(10))
  File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument


The correct way to do that is:

>>> f(4,(i for i in range(10)))
4 (<generator object at 0xb7dab56c>,)

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/





More information about the Python-list mailing list