Generator expression parenthesis mixed with function call ones
Laurent Pointal
laurent.pointal at limsi.fr
Wed Mar 7 11:15:33 EST 2007
Facundo Batista a écrit :
> 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))
How a Python beginner know that he is using a generator and not a
list-comprehension ?
>> 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>,)
Thanks, I know. My example is just to show a point I consider to be
non-coherent (different processing if there is one argument or more than
one).
A+
Laurent.
More information about the Python-list
mailing list