Generator expression parenthesis mixed with function call ones

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Mar 7 11:23:18 EST 2007


En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal  
<laurent.pointal at limsi.fr> escribió:

>>>> f(4,i for i in range(10))
>   File "<stdin>", line 1
> SyntaxError: invalid syntax

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

> Why does Python allow generator expression parenthesis to be mixed with
> function call parenthesis when there is only one parameter ?

Because they are redundant when only one argument is used.
sum(i for i in range(10)) looks better than sum((i for i in range(10)))
"Beautiful is better than ugly", and "Readability counts."

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

It's similar to "%d" % 123 vs. "%d" % (123,)
"""Special cases aren't special enough to break the rules.
Although practicality beats purity."""

> User should all times explicitly use () for a generator expression and
> [] for a list comprehension expression.

For a list comprehension, yes. For a generator, not always.

-- 
Gabriel Genellina




More information about the Python-list mailing list