[Python-ideas] Consider making enumerate a sequence if its argument is a sequence
M.-A. Lemburg
mal at egenix.com
Wed Sep 30 20:43:30 CEST 2015
On 30.09.2015 20:26, Andrew Barnert via Python-ideas wrote:
> On Sep 30, 2015, at 11:11, M.-A. Lemburg <mal at egenix.com> 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.
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Sep 30 2015)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
________________________________________________________________________
2015-09-25: Started a Python blog ... ... http://malemburg.com/
2015-10-21: Python Meeting Duesseldorf ... 21 days to go
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list