[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-18 Thread Marco Sulla
On Wed, 18 Nov 2020 at 00:31, Oscar Benjamin wrote: > I can write many things myself. That doesn't mean that it wouldn't be > good if someone already wrote it for me (and for everyone else). > What about more_itertools? ___ Python-ideas mailing list --

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Oscar Benjamin
On Tue, 17 Nov 2020 at 22:35, Paul Moore wrote: > > On Tue, 17 Nov 2020 at 22:19, Oscar Benjamin > wrote: > > It would be nice if islice gave an object that supported slicing so > > that you could spell it like: > > > >for x in islice(a)[5:]: > > > > I find it hard to decipher the meaning of

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Steven D'Aprano
On Tue, Nov 17, 2020 at 10:17:32PM +, Oscar Benjamin wrote: > It would be nice if islice gave an object that supported slicing so > that you could spell it like: > >for x in islice(a)[5:]: > > I find it hard to decipher the meaning of the arguments to islice > compared to reading a norma

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Paul Moore
On Tue, 17 Nov 2020 at 22:19, Oscar Benjamin wrote: > It would be nice if islice gave an object that supported slicing so > that you could spell it like: > >for x in islice(a)[5:]: > > I find it hard to decipher the meaning of the arguments to islice > compared to reading a normal slice expres

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Oscar Benjamin
> > On Tue, 17 Nov 2020 at 10:35, Nuri Jung wrote: > >> > >> How about enabling subscription operator (`[]`) for generator expressions? > >> Also for all `zip()`, `key()`, etc. They could be evaluated in the > >> background only for the requested amount, to avoid evaluating the whole > >> expre

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Steven D'Aprano
On Tue, Nov 17, 2020 at 03:42:54AM -, Nuri Jung wrote: > How about enabling subscription operator (`[]`) for generator expressions? Generator expressions are iterators, and the iterator protocol is intentionally very simple. You only need to provide two things for an object to be an iterato

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Nuri Jung
I agree with your detailed explanation, and it would be a great idea to add a keyword argument to the `next()` function. Just for reference, I believe C++ also has similar function, `std::next()` which advances 'iterators', and it also works on non-indexible (i.e. linked list, etc.) containers.

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Paul Moore
>>> from itertools import islice >>> a = (i for i in range(0, 100, 10)) >>> next(islice(a, 5, None)) Paul On Tue, 17 Nov 2020 at 15:37, Joao S. O. Bueno wrote: > > Although that is not a pattern I recall I had needed, but for the first item > in a generator, > I recognize it is more complicated

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Chris Angelico
On Wed, Nov 18, 2020 at 2:35 AM Joao S. O. Bueno 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 chan

[Python-ideas] Re: Enable subscription operator for generator expressions

2020-11-17 Thread Joao S. O. Bueno
Although that is not a pattern I recall I had needed, but for the first item in a generator, I recognize it is more complicated than it should to be able to do that. However, not only that would be too big a change for all this objects I think one would expect an object providing index access wit