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

Chris Barker chris.barker at noaa.gov
Wed Sep 30 19:28:48 CEST 2015


On Wed, Sep 30, 2015 at 10:19 AM, Neil Girdhar <mistersheik at gmail.com>
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?
>

well, range is simpler -- you don't pass arbitrary iterables into it. It
always has to compute integer values according to start, stop, step -- easy
to implement as either iteration or indexing.

enumerate, on the other hand, takes an arbitrary iterable -- so it can't
just index into that iterable if asked for an index.

You are right, of course, that it COULD do that if it was passed a sequence
in the first place, but then you have an intera e whereby you get a
different kind of object depending on how you created it, which is pretty
ugly.

But again, we could add indexing to enumerate, and have it do the ugly
inefficient thing when it's using an underlying non-indexable iterator, and
do the efficient thing when it has a sequence to work with, thereby
providing the same API regardless.

-CHB





> On Wed, Sep 30, 2015 at 1:18 PM Neil Girdhar <mistersheik at gmail.com>
> wrote:
>
>> Ah good point.  Well, in the case of a sequence argument, an enumerate
>> object could be both a sequence and an iterator.
>>
>> On Wed, Sep 30, 2015 at 1:15 PM Alexander Belopolsky <
>> alexander.belopolsky at gmail.com> wrote:
>>
>>>
>>> On Wed, Sep 30, 2015 at 12:53 PM, Neil Girdhar <mistersheik at gmail.com>
>>> wrote:
>>>
>>>> A Sequence is an Iterator.
>>>
>>>
>>> No, a Sequence is an Iterable, not an Iterator:
>>>
>>> >>> issubclass(collections.Sequence, collections.Iterator)
>>> False
>>> >>> issubclass(collections.Sequence, collections.Iterable)
>>> True
>>>
>>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150930/70d4129b/attachment.html>


More information about the Python-ideas mailing list