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

David Mertz mertz at gnosis.cx
Sat Dec 1 11:27:31 EST 2018


A proposal to make map() not return an iterator seems like a non-starter.
Yes, Python 2 worked that way, but that was a long time ago and we know
better now.

In the simple example it doesn't matter much:

  mo = map(lambda x: x, "aardvark")

But map() is more useful for the non-toy case:

  mo = map(expensive_db_lookup, list_of_keys)

list_of_keys can be a concrete list, but I'm using map() mainly
specifically to get lazy iterator behavior.

On Sat, Dec 1, 2018, 11:10 AM Paul Svensson <paul-python at svensson.org wrote:

> On Sat, 1 Dec 2018, Steven D'Aprano wrote:
>
> > On Thu, Nov 29, 2018 at 08:13:12PM -0500, Paul Svensson wrote:
> >
> >> What's being proposed is simple, either:
> >>  * len(map(f, x)) == len(x), or
> >>  * both raise TypeError
> >
> > Simple, obvious, and problematic.
> >
> > Here's a map object I prepared earlier:
> >
> > from itertools import islice
> > mo = map(lambda x: x, "aardvark")
> > list(islice(mo, 3))
> >
> > If I now pass you the map object, mo, what should len(mo) return? Five
> > or eight?
>
> mo = "aardvark"
> list(islice(mo, 3))
>
> By what magic would the length change?
> Per the proposal, it can only be eight.
> Of course, that means mo can't, in this case, be an iterator.
> That's what the proposal would change.
>
>         /Paul
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181201/2308aed3/attachment-0001.html>


More information about the Python-ideas mailing list