learning to use iterators

Terry Reedy tjreedy at udel.edu
Tue Dec 23 18:07:40 EST 2014


On 12/23/2014 4:25 PM, Ben Finney wrote:
> Ian Kelly <ian.g.kelly at gmail.com> writes:
>
>> On Tue, Dec 23, 2014 at 11:55 AM, Seb <spluque at gmail.com> wrote:
>>> Particulary, what do the parentheses do there?
>>
>> The parentheses enclose a generator expression, which is similar to a
>> list comprehension [1] but produce a generator, which is a type of
>> iterator, rather than a list.
>
> To be clear: there's nothing about parentheses that produce a generator
> expression.

Incorrect; parentheses *are* as a part of 'generator expression'.  From 
the doc:
generator_expression ::=  "(" expression comp_for ")"

> The generator expression is produced by the syntax used, such as
> ‘frob(spam) for spam in collection_of_spam if spam > 0’.

This is (intentionally) not a generator expression.

a = i for i in range(39)

is a syntax error, whereas

t = a, 3, '3'

binds 't' to the tuple a, 3, '3'.

The parentheses that are part of a generator expression may only be 
omitted in a call with one argument.  See my other response.

-- 
Terry Jan Reedy





More information about the Python-list mailing list