On 30.09.2015 20:26, Andrew Barnert via Python-ideas wrote:
On Sep 30, 2015, at 11:11, M.-A. Lemburg firstname.lastname@example.org wrote:
On 30.09.2015 19:19, Neil Girdhar wrote: I guess, I'm just asking for enumerate to go through the same change that range went through. Why wasn't it a problem for range?
range() returns a list in Python 2 and a generator in Python 3.
No it doesn't. It returns a (lazy) sequence. Not a generator, or any other kind of iterator.
You are right that it's not of a generator type and more like a lazy sequence. To be exact, it returns a range object and does implement the iter protocol via a range_iterator object.
In Python 2 we have the xrange object which has similar properties, but not the same, e.g. you can't slice it.
I don't know why so many people seem to believe it returns a generator. (And, when you point out what it returns, most of them say, "Why was that changed from 2.x xrange, which returned a generator?" but xrange never returned a generator either--it returned a lazy almost-a-sequence from the start.)
Perhaps because it behaves like one ? :-)
Unlike an iterator, it doesn't iterate over a sequence, but instead generates the values on the fly.
FWIW: I don't think many people use the lazy sequence features of range(), e.g. the slicing or index support. By far most uses are in for-loops.