On Wed, Nov 18, 2020 at 2:35 AM Joao S. O. Bueno <jsbu...@python.org.br> wrote:
> Also, see it as potentially making a lot of code error-prone:
> let's say one gets passed a generator where a sequence is expected.
> In current Python, if an item is accessed by index, one just get an explicit
> IndexError. If objects change to having indexes, two consecutive access
> to `gen[1]`  will consume the generator and return different values. That
> could be very confusing.

Extremely confusing, and I think that would be enough to kill the idea.

> On the other hand, as I said, I can't come up with
> a simple pattern to get the nth item - so probably we
> should think of an easy and performant way.

The very concept of "the nth item" doesn't work with generators, so I
think there's little reason to try to define it usefully. However...

> One way I can think of is to have a named parameter
> to the `next` built-in that would allow one to move forward more than one
> position.
>
> Say: `fith_element = next(gen, skip=4) `

(Ohh, the strong fifth element - Boron?)

> and finally, one way I could think of retrieving the n
> element is:
>
> In [19]: a = (i for i in range(0, 100, 10))
>
> In [20]: next(b for i, b in enumerate(a) if i==5)
> Out[20]: 50
>
> It definitely feels like there should be a simpler way,
> but I just could not come up with it.

... there may be some value in this simple "skip" option. For the
record, the most normal way to do this sort of thing would be the
islice function:

https://docs.python.org/3/library/itertools.html#itertools.islice

but if all you want to do is "skip the next four, then take the next
one after that", it would be convenient to quickly pump the generator
a few times before returning a value.

This isn't something I often need personally, but I can definitely see
the value of it. +0.5; this does get asked for a good bit, and a
keyword argument on next() would be a lot less confusing than directly
subscripting a generator.

ChrisA
_______________________________________________
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/5L2BIH2OZZTHIATKRDDKNLLJDPY5TACU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to