[Python-ideas] __len__() for map()

Steven D'Aprano steve at pearwood.info
Sat Dec 1 12:23:07 EST 2018

On Sat, Dec 01, 2018 at 12:06:23PM -0500, David Mertz wrote:

> Given that the anti-fix is just as simple and currently available, I don't
> see why we'd want a change:
> # map->sequence
> mo = list(mo)
> FWIW, I actually do write exactly that code fairly often, it's not hard.

Sure, but that makes a copy of the original data and means you lose the 
benefit of map being lazy.

Naturally we will always have the ability to call list and eagerly 
convert to a sequence, but these proposals are for a way of getting the 
advantages of sequence-like behaviour while still keeping the advantages 
of laziness.

With iterators, the only way to get that advantage of laziness is 
to give up the ability to query length, random access to items, etc even 
when the underlying data is a sequence and that information would have 
been readily available. We can, at least sometimes, have the best of 
both worlds. Maybe.


More information about the Python-ideas mailing list