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

Chris Angelico rosuav at gmail.com
Mon Nov 26 17:36:08 EST 2018


On Tue, Nov 27, 2018 at 9:15 AM Jonathan Fine <jfine2358 at gmail.com> wrote:
> Briefly, I don't like your suggestion because many important iterables
> don't have a length!

That part's fine. The implication is that mapping over an iterable
with a length would give a map with a known length, and mapping over
something without a length wouldn't.

But I think there are enough odd edge cases (for instance, is it okay
to call the function twice if you __getitem__ twice, or should you
cache it?) that it's probably best to keep the built-in map() simple
and reliable. Don't forget, too, that map() can take more than one
iterable, and some may not have lengths. (You can define enumerate in
terms of map and itertools.count; what is the length of the resulting
enumeration?) If you want a map-like object that takes specifically a
single list, and is a mapped view to that list, then go for it - but
that can be its own beast, not related to the map() built-in function.

Also, it may be of value to check out more-itertools; you might find
something there that you like.

ChrisA


More information about the Python-ideas mailing list