Why can you omit the surrounding parentheses for generators in Python when passing it to a function?

I was just experimenting in Python with different syntax for passing in a generator as an argument to a function, and I realized that although I did,

>>> sum((j for j in xrange(5))) 10 

this also works:

 >>> sum(j for j in xrange(5)) 10 

This is tested on Python 2.6.6 on Linux. What happens under the hood? Is it just syntactic sugar? In the end, a commonly deployed generator inaudibly interprets:

 >>> j for j in xrange(5) File "<stdin>", line 1 j for j in xrange(5) ^ SyntaxError: invalid syntax 
+6
python generator syntax language-implementation
source share
1 answer

I am sure reading python grammar will answer this question.

If you prefer plain English over grammar: PEP-289 explains this.

+5
source share

All Articles