[Python-ideas] Suggested MapView object (Re: __len__() for map())

Paul Moore p.f.moore at gmail.com
Thu Dec 13 04:25:26 EST 2018


On Thu, 13 Dec 2018 at 05:55, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>
> Chris Angelico wrote:
> > On Thu, Dec 13, 2018 at 3:07 PM Chris Barker - NOAA Federal via
> > Python-ideas <python-ideas at python.org> wrote:
> >
> >>>>>  obj is iter(obj)
> >>
> >>Is that a hard and fast rule?
> > Yes, it is.
> >
> > https://docs.python.org/3/library/stdtypes.html#iterator-types
>
> The docs aren't very clear on this point. They claim this is necessary
> so that the iterator can be used in a for-loop, but that's obviously
> not strictly true, since a proxy object could also be used.

See also https://docs.python.org/3.7/glossary.html#term-iterator,
which reiterates the point that "Iterators are required to have an
__iter__() method that returns the iterator object itself". By that
point, I'd say the docs are pretty clear...

> They also make no mention about whether one should be able to rely
> on this as a definitive test of iterator-ness.

That glossary entry is linked from
https://docs.python.org/3.7/library/collections.abc.html#collections.abc.Iterator,
so it would be pretty hard to argue that it's not part of the
"definitive test of iterator-ness".

> In any case, I don't claim that my MapView implements the full
> iterator protocol, only enough of it to pass for an iterator in
> most likely scenarios that assume one.

But not enough that it's legitimate to describe it as an "iterator".
It may well be a useful class, and returning it from a map-like
function may be a practical and effective thing to do, but describing
it as an "iterator" does nothing apart from leading to distracting
debates on how it doesn't work the same as an iterator. Better to just
accept that it's *not* an iterator, and focus on whether it's
useful...

IMO, it sounds like it's useful, but it's not backward compatible
(because it's not an iterator ;-)). Whether it's *sufficiently* useful
to justify breaking backward compatibility is a different discussion
(all I can say on that question is that I've never personally had a
case where the current Python 3 behaviour of map is a problem).

Paul


More information about the Python-ideas mailing list