R. David Murray <rdmur...@bitdance.com> added the comment: 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. ---------- nosy: +r.david.murray resolution: -> invalid status: open -> closed _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue5968> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com