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

Paul Svensson paul-python at svensson.org
Sat Dec 1 11:07:53 EST 2018


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


More information about the Python-ideas mailing list