I think there is also a distinction about the *current* meaning
of "required" to be made, in "[i]terators
are required to have an __iter__()
method": "required" doesn't specify whether this is:
1. by convention, and doing otherwise is just some form of
undefined behaviour; for a human (or perhaps type-checker) reading
it to think it's an iterator, it needs `__iter__`, but it's really
something like passing an object of the wrong type to an unbound
method - unenforced by the language (it used to be illegal in Py2)
2. in some way actually enforced: the iterator is required to
have `__iter__` that returns self, and
While 1 is clearly what actually happens in CPython, was that the
intended meaning? I'd think so - 1 is still a perfectly acceptable
interpretation of "required" (even if "required" isn't the most
clear way of expressing it). Even if it wasn't the original
meaning, that's how I think it should now be interpreted because
that's what it is de facto.
Do we know who originally wrote that line, so we could ask them?
(The furthest I've traced it is
https://github.com/python/cpython/commit/f10aa9825e49e8652f30bc6d92c736fe47bb134c
but I don't have any knowledge of SVN or CVS (whichever was used
at the time) to go further.)
Also, any user-defined iterator that doesn't also define __iter__
would be considered wrong and nobody would refuse to fix that. If
it's already a bug anyway, why bother changing the behaviour and
check that?
PatrickA. It's not an iterator if it doesn't define `__next__`.
B. It is strongly recommended that iterators also define `__iter__`.
In "standards" language, I think (A) is MUST and (B) is merely OUGHT or maybe SHOULD.
On Tue, Sep 14, 2021 at 12:30 PM Brett Cannon <brett@python.org> wrote:
Over in https://github.com/python/typeshed/issues/6030 I have managed to kick up a discussion over what exactly an "iterator" is. If you look at https://docs.python.org/3/library/functions.html#iter you will see the docs say it "Return[s] an iterator object." Great, but you go the glossary definition of "iterator" at https://docs.python.org/3/glossary.html#term-iterator you will see it says "[i]terators are required to have an__iter__()
method" which neither `for` nor `iter()` actually enforce.
Is there something to do here? Do we loosen the definition of "iterator" to say they should define __iter__? Leave it as-is with an understanding that we know that it's technically inaccurate for iter() but that we want to encourage people to define __iter__? I'm assuming people don't want to change `for` and `iter()` to start requiring __iter__ be defined if we decided to go down the "remove the __aiter__ requirement" from aiter() last week.
BTW all of this applies to async iterators as well.