Author r.david.murray
Recipients r.david.murray, svenrahmann
Date 2009-05-08.14:10:17
SpamBayes Score 4.44152e-06
Marked as misclassified No
Message-id <1241791820.49.0.420788047999.issue5968@psf.upfronthosting.co.za>
In-reply-to
Content
This is not a bug.  It's not even a doc bug, IMO.

When you do

  num1 = [x for x in range(0, 6)]

it is not that you are assigning a list comprehension to num1, what you
are doing is running a list comprehension to create an actual list,
which is what gets assigned to num1.  The docs are pretty clear about
that, I think.  So yes you can iterate over a list multiple times,
because of how it implements the iteration protocol. 

On the other hand, when you do

  num3 = (x for x in range(0, 6))

you create a generator object, which is what gets assigned to num3. 
Generators created by generator expressions can only be iterated over
until they are exhausted.  That is a major point of their existence:
producing one item at a time on demand and not saving them.

A range object is its own special case, and is neither a list nor a
generator.  It is reusable, as you found.

None of this should be documented in the 'for' statement.  The for
statement explains the protocol it follows.  What happens when you use
it to iterate over any given object depends on how that object
impelements the iteration protcol.  So you have to look to the
documentation of those objects for further enlightenment, I'm afraid.
History
Date User Action Args
2009-05-08 14:10:20r.david.murraysetrecipients: + r.david.murray, svenrahmann
2009-05-08 14:10:20r.david.murraysetmessageid: <1241791820.49.0.420788047999.issue5968@psf.upfronthosting.co.za>
2009-05-08 14:10:19r.david.murraylinkissue5968 messages
2009-05-08 14:10:17r.david.murraycreate