[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-10-29 Thread Brett Cannon
FYI I opened https://github.com/python/cpython/pull/29170 to loosen/correct the definition of "iterator", but I got push-back on the PR and this thread never reached a clear conclusion. As such I'll ask the SC to make a call. ___ Python-Dev mailing

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-20 Thread Brett Cannon
On Sun, Sep 19, 2021 at 8:15 AM Steve Holden wrote: > I understood that _iterables_ are required to have an __iter__ method, not > iterators. > > Therefore, are we simply discussing whether all iterators should be > iterable? > At this point it's more about how to document this. > At the

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-19 Thread Steve Holden
I understood that _iterables_ are required to have an __iter__ method, not iterators. Therefore, are we simply discussing whether all iterators should be iterable? At the moment the CPython implementation does't require that AFAIK. regards Steve On Tue, Sep 14, 2021 at 8:39 PM Guido van Rossum

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-16 Thread Terry Reedy
On 9/16/2021 3:02 AM, Paul Moore wrote: The debate here is (I think!) whether an *iterator* that is not also an *iterable* is a valid iterator. This framing of the question seems biased in that it initially uses 'iterator' to mean 'object with __next__ but not __iter__' whe the propriety of

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-16 Thread Brett Cannon
On Wed, Sep 15, 2021 at 4:06 PM Guido van Rossum wrote: > [SNIP] > Reminder about how for-loops work: > > This: > > for x in seq: > > > translates (roughly) to this: > > _it = iter(seq) > while True: > try: > x = next(_it) > except StopIteration: > break > >

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-16 Thread Paul Moore
On Thu, 16 Sept 2021 at 01:30, Chris Barker via Python-Dev wrote: > """ > "[i]terators are required to have an __iter__() method" which neither `for` > nor `iter()` actually enforce. > """ > > I'm confused -- as far as I can tell `for` does enforce this -- well, it > doesn't enforce it, but it

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Steven D'Aprano
On Wed, Sep 15, 2021 at 04:01:31PM -0700, Guido van Rossum wrote: > Steven's class A is the kind of class a custom sequence might return from > its __iter__ method. E.g. > > class S: > def __iter__(self): > return A() Correct, where A itself has a `__next__` method but no `__iter__`

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Steven D'Aprano
On Wed, Sep 15, 2021 at 08:57:58AM -0700, Guido van Rossum wrote: [...] > Yes, we all understand that. The reason I invoked "duck typing" is that as > long as you don't use the iterator in a situation where iter() is called on > it, it works fine. Just like a class with a readline() method works

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Chris Barker via Python-Dev
Note: I am all for not enforcing anything here -- let's keep duck typing alive! If static type checkers want to be more pedantic, they can be -- that's kinda what they are for :-) But the OP wrote: """ "[i]terators are required to have an __iter__()

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Guido van Rossum
On Wed, Sep 15, 2021 at 3:54 PM Ethan Furman wrote: > Guido: > > It's still an iterator, since it duck-types in most cases where an > iterator > > is required (notably "for", which is the primary use case for the > iteration > > protocols -- it's in the first sentence of PEP 234's abstract).

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Ethan Furman
Guido: > It's still an iterator, since it duck-types in most cases where an iterator > is required (notably "for", which is the primary use case for the iteration > protocols -- it's in the first sentence of PEP 234's abstract). D'Aprano: > I don't think it duck-types as an iterator. Here's an

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Terry Reedy
On 9/15/2021 12:33 AM, Guido van Rossum wrote: On Tue, Sep 14, 2021 at 9:03 PM Steven D'Aprano > wrote: On Tue, Sep 14, 2021 at 12:33:32PM -0700, Guido van Rossum wrote: > My view of this is: > > A. It's not an iterator if it doesn't define

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 11:44 PM Steven D'Aprano wrote: > On Tue, Sep 14, 2021 at 09:38:38PM -0700, Guido van Rossum wrote: > > > > I don't know what I would call an object that only has __next__, > > > apart from "broken" :-( > > > > > > > It's still an iterator, since it duck-types in most

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Steven D'Aprano
On Tue, Sep 14, 2021 at 04:50:05PM -0700, Guido van Rossum wrote: > TBH I don't think there is an *actual* problem here. I think it's just > about choosing the right wording for the glossary (which IMO does not have > status as a source of truth anyway). +1 -- Steve

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-15 Thread Steven D'Aprano
On Tue, Sep 14, 2021 at 09:38:38PM -0700, Guido van Rossum wrote: > > I don't know what I would call an object that only has __next__, > > apart from "broken" :-( > > > > It's still an iterator, since it duck-types in most cases where an iterator > is required (notably "for", which is the

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 9:31 PM Steven D'Aprano wrote: > If it helps, I have tons of code that tests for iterators using: > > iter(obj) is obj > > That has been a documented requirement for the iterator protocol > forever. Its in the PEP. > > "A class that wants to be an iterator should

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 9:03 PM Steven D'Aprano wrote: > On Tue, Sep 14, 2021 at 12:33:32PM -0700, Guido van Rossum wrote: > > My view of this is: > > > > A. It's not an iterator if it doesn't define `__next__`. > > > > B. It is strongly recommended that iterators also define `__iter__`. > > > >

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Steven D'Aprano
If it helps, I have tons of code that tests for iterators using: iter(obj) is obj That has been a documented requirement for the iterator protocol forever. Its in the PEP. "A class that wants to be an iterator should implement two methods: a next() method that behaves as described above,

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Steven D'Aprano
On Tue, Sep 14, 2021 at 12:33:32PM -0700, Guido van Rossum wrote: > My view of this is: > > A. 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

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Brandt Bucher
Guido van Rossum wrote: > TBH I don't think there is an *actual* problem here. I think it's just > about choosing the right wording for the glossary (which IMO does not have > status as a source of truth anyway). Good point. I'm probably approaching this from the wrong angle (by trying to "fix"

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 4:33 PM Brandt Bucher wrote: > Guido van Rossum wrote: > > On Tue, Sep 14, 2021 at 3:49 PM Brandt Bucher brandtbuc...@gmail.com > > wrote: > > > I think it's also worth noting that a missing "`__iter__` that returns > > > self" is trivial to recover from... just use a new

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Brandt Bucher
Guido van Rossum wrote: > On Tue, Sep 14, 2021 at 3:49 PM Brandt Bucher brandtbuc...@gmail.com > wrote: > > I think it's also worth noting that a missing "`__iter__` that returns > > self" is trivial to recover from... just use a new reference to the > > iterator instead. The overhead of a method

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 3:49 PM Brandt Bucher wrote: > I think it's also worth noting that a missing "`__iter__` that returns > self" is trivial to recover from... just use a new reference to the > iterator instead. The overhead of a method call for this convention almost > seems silly. > The

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Brandt Bucher
I think it's also worth noting that a missing "`__iter__` that returns self" is trivial to recover from... just use a new reference to the iterator instead. The overhead of a method call for this convention almost seems silly. What worries me most about changing the current "requirement" is

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Patrick Reader
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

[Python-Dev] Re: Should the definition of an "(async) iterator" include __iter__?

2021-09-14 Thread Guido van Rossum
My view of this is: A. 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 wrote: > Over