
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