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

Steven D'Aprano steve at pearwood.info
Tue Dec 11 20:24:27 EST 2018

On Wed, Dec 12, 2018 at 11:31:03AM +1300, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >I suggest we provide a separate mapview() type that offers only the lazy 
> >sequence API, without trying to be an iterator at the same time.
> Then we would be back to the bad old days of having two functions
> that do almost exactly the same thing.

They aren't "almost exactly the same thing". One is a sequence, which is 
a rich API that includes random access to items and a length; the other 
is an iterator, which is an intentionally simple API which fails to meet 
the needs of some users.

> My suggestion was made in
> the interests of moving the language in the direction of having
> less warts, rather than adding more or moving the existing ones
> around.
> I acknowledge that the dual interface is itself a bit wartish,

It's a "bit wartish" in the same way that the sun is "a bit warmish".

> but it's purely for backwards compatibility

And it fails at that too.

x = map(str.upper, "abcd")
x is iter(x)

returns True with the current map, an actual iterator, and False with 
your hybrid.

Current map() is a proper, non-broken iterator; your hybrid is a broken 
iterator. (That's not me being derogative: its the official term for 
iterators which don't stay exhausted.)

I'd be more charitable if I thought the flaws were mere bugs that could 
be fixed. But I don't think there is any way to combine two incompatible 
interfaces, the sequence and iterator APIs, into one object without 
these sorts of breakages.

Take the __next__ method out of your object, and it is a better version 
of what I proposed earlier. With the __next__ method, its just broken.


More information about the Python-ideas mailing list