[Python-ideas] Consider making enumerate a sequence if its argument is a sequence

Steven D'Aprano steve at pearwood.info
Thu Oct 1 03:15:06 CEST 2015


On Wed, Sep 30, 2015 at 05:19:53PM +0000, 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?

There is a pernicious myth that (x)range is an iterator. It is not. It 
is a sequence, but one where the items are calculated on demand rather 
than pre-populated into some large data structure (a list or array).

This is not just a matter of labels. It is a matter of the actual 
behaviour. (x)range objects don't behave like iterators except in the 
simplest sense that you can iterate over them.

So your question is based on false assumptions -- range didn't go 
through any such change. In Python 2, range was eager and xrange lazy, 
but both are sequences, and in Python 3 the eager version is gone and 
the lazy version renamed without the "x" prefix.




-- 
Steve


More information about the Python-ideas mailing list