9

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've been doing this,

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

this works as well:

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

This is tested on Python 2.6.6 on Linux. What's going on under the hood? Is it just syntactic sugar? After all, usually an unwrapped generator is indecipherable to the interpreter:

>>> j for j in xrange(5)
  File "<stdin>", line 1
    j for j in xrange(5)
        ^
SyntaxError: invalid syntax
Darren Yin
  • 628
  • 3
  • 10
  • I noticed this as well, and it actually kind of irritates me. I don't like grammar irregularities like this, even if they are nice syntactic sugar. If I wanted a bizarre grammar, I'd be programming in perl. – Omnifarious Jan 25 '11 at 22:29
  • @Omnifarius: I value consistency highly (and so does Python generally), but double parens are just plain redundant. –  Jan 25 '11 at 22:29
  • @delnan - They are not. For example: `sum(j for j,k in {'a':1, 'b':2}.iteritems())`. How is the comma between the `j` and `k` interpreted, and why? – Omnifarious Jan 25 '11 at 22:32
  • `print False,True or True,False` – Apalala Jan 25 '11 at 23:06
  • 1
    @Omnifarius: It's interpreted as tuple unpacking. Why - are you asking for the detail of the grammar that allows this or why it was designed this way? For the latter: Because iteration over tuples (and therefore tuple unpacking in a `for`) is relatively common, so requiring parens on it would be rather cumbersome. (For the former: After seeing the start of a generator expression, the parser expects an identifier or some comma-seperated identifiers - what comes next qualifies as such, so it goes with that) –  Jan 26 '11 at 13:43

1 Answers1

6

I'm sure reading the python grammar will answer that question.

If you prefer plain English over grammars: PEP-289 explains it.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 7
    The Python grammar, while not nearly as bad as Perl's, is not for the faint of heart. The shorter answer is "yes, syntactic sugar". –  Jan 25 '11 at 22:19
  • The grammar will tell you *that* you can; it won't tell you *why* it's designed that way. – Glenn Maynard Jan 25 '11 at 22:38
  • 4
    In addition to the grammar, you probably want to read PEP 289. – Glenn Maynard Jan 25 '11 at 22:40
  • 1
    Thanks! Part 2 of the details of PEP 289 was pretty much what I was looking for. On the other hand, I was looking through the grammar, and it looks like the line testlist_comp is the one that allows this construct. Any idea why it's called testlist_comp now and not testlist_gexp as in the PEP? – Darren Yin Jan 26 '11 at 18:40
  • 1
    This answer could be improved if you summarized the relevant content of the links you provide. – Vaelus Apr 11 '19 at 01:59