Am 30.05.21 um 19:08 schrieb Guido van Rossum:
Returning "self" as the iterator was originally only intended to paper
over the case where you want to write
it = iter(a)
<maybe call next(it) a few times>
for x in it:
...
-- basically we wanted 'for x in iter(a)' and 'for x in a' to have the
same meaning.
The above use case (iterator being iterable themselves) was a very good
design decision. In fact, I have a blog post dating back from 2006 where
I berated Java from not doing the same:
https://rittau.org/2006/11/java-iterators-are-not-iterable/. To take my
example from there converted to python:
class Tree:
def depth_first() -> Iterator[...]: ...
def breath_first() -> Iterator[...]: ...
for item in tree.depth_first(): ...
This example would not work if "iter(it)" would not return "self".
- Sebastian
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/VWLREDZR6HXXKBGTWRXV6H3FU7QZQWOQ/
Code of Conduct: http://python.org/psf/codeofconduct/