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
python generator syntax language-implementation
Darren yin
source share