On Tue, Oct 6, 2020 at 18:16 Steven D'Aprano <st...@pearwood.info> wrote:
> For `__advance__` to be an official Python protocol, it would almost > certainly have to be of use for *general purpose iterators*, not just > specialised ones -- and probably not *hypothetical* iterators which may > not even exist. Do you have concrete examples of your skip list and tree > iterators that are in wide-spread use? Yeah, I’m still waiting for the real use case to be revealed. (There my well be one.) Specialised iterators can create whatever extra APIs they want to > support, but the official iterator protocol intentionally has a very > basic API: > > - anything with an `__iter__` method which returns itself; > - and a `__next__` method that returns the next value, raising > StopIteration when exhausted. > > This is a bare minimum needed to make an iterator, and we like it that > way. For starters, it means that generators are iterators. There’s a precedent though, __length_hint__ (PEP 424). The OP anticipated this, with “[a] function which dispatches to a dunder __advance__ method (if one exists) or, as a fallback, calls next repeatedly.” Clearly the function would be on a par with len() and next(). So it would be optional to implement but the operation would be available for all iterators (even generators). If people want to supply objects that support the iterator protocol but also offer a rich API including: > > - peek > - restart > - previous > - jump ahead (advance) > > all features that have been proposed, there is nothing stopping you from > adding those features to your iterator classes. But they all have > problems if considered to be necessary for *all* iterators. Strawman, since all except advance() would require some kind of buffering to build them out of the basics. I would expect that, given a sufficiently compelling real-world > use-case, we would be prepared to add a jump ahead method to > list-iterators, as a specific feature of that iterator, not of all > iterators. But the advance() function could support all iterators using the OP’s fallback. —Guido -- --Guido (mobile)
_______________________________________________ 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/4VOHC5TZFDWSQ3GESAJN5KCDN5XAOEQ4/ Code of Conduct: http://python.org/psf/codeofconduct/