On Thu, Jan 21, 2021 at 3:13 PM Christopher Barker <python...@gmail.com> wrote:
>
> On Thu, Jan 21, 2021 at 10:43 AM Neil Girdhar <mistersh...@gmail.com> wrote:
>>
>> > > I've seen this proposed here before.  The general idea is that some 
>> > > iterator transformations (like enumerate) should return sequences when 
>> > > they're applied to sequences.
>
>
> I'm not so sure -- I don't think I want a sequence returned. In fact, in this 
> case, just the opposite. If you wanted a sequence, you could simply do:
>
> reversed(list(enumerate(the_iterable)))
>
> Rather, you want (or I want, anyway) is for various iterators to be able to 
> do things that require a Sequence, when given a Sequence. Or, maybe not even 
> a full Sequence, but rather, some particular aspect of a sequence -- like the 
> length, or indexability.
>
> enumerate() with a reversed flag is one. Another, I'd like is islice to 
> support negative indexing for indexing from the end. There are probably many 
> others.
>
> I'd love to see these supported. For this example:
>
> enumerate(an_iterable, reversed=True)

Don't you think this is over-complicated?

It's possible for reversed(enumerate(...)) to just work if enumerate
of a sequence were to return a sequence view.  Then you would also get
all the other sequence operations for free like enumerate(...)[23:27],
len(enumerate(...)), etc.

>
> would "just work" if an_iterable had a __len__ and a __getitem__.
>
> It would raise a TypeError if passed a non-lengthed, or non-indexable 
> iterable.
>
> -CHB
>
> > > In short, this has nothing to do with reversed.
>
> I agree.
>
> >> If you made enumerate return a sequence when its input is a sequence, you 
> >> would also be able to do enumerate(some_list)[34],
>
> As Chris A mentioned, it's quite easy to wrap list() or tuple() around it if 
> you want that.

Yes, you can alternatively wrap with list and pay the computational
cost.  However, as was suggested by Andrew in this thread, but has
come up many times before, the native iteration tools could return
views.  This saves the user having to build her own custom classes
(along with tests, etc.).  I happen to think this is simpler and more
beautiful.

Best,

Neil

>
> - Chris B
>
> --
> Christopher Barker, PhD (Chris)
>
> Python Language Consulting
>   - Teaching
>   - Scientific Software Development
>   - Desktop GUI and Web Development
>   - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/S4BNJSIRGJT3IKK7GQCHWUWK2VLDYE4P/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to