Why can you omit the surrounding parentheses for generators in Python when passing it into a function?
Asked Answered
V

1

10

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
Vineland answered 25/1, 2011 at 22:15 Comment(5)
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.Castellated
@Omnifarius: I value consistency highly (and so does Python generally), but double parens are just plain redundant.Patrinapatriot
@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?Castellated
print False,True or True,FalsePetula
@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)Patrinapatriot
N
6

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

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

Nigrescent answered 25/1, 2011 at 22:16 Comment(5)
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".Patrinapatriot
The grammar will tell you that you can; it won't tell you why it's designed that way.Pawsner
In addition to the grammar, you probably want to read PEP 289.Pawsner
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?Vineland
This answer could be improved if you summarized the relevant content of the links you provide.Malapropism

© 2022 - 2024 — McMap. All rights reserved.