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

Kale Kundert kale at thekunderts.net
Mon Nov 26 20:44:55 EST 2018


>
> Equally obvious and valid answer: The length is the smallest length of
> its iterables, ignoring any infinite ones, but if any iterable is
> unsized, the map is unsized.
>
> And both answers will surprise people.
>
> I still think there's room in the world for a "mapped list view" type,
> which retains a reference to an underlying list, plus a function, and
> proxies everything through to the function. It would NOT have the
> flexibility of map(), but it would be able to directly subscript, it
> wouldn't need any cache, etc, etc.
>
> ChrisA
> _______________________________________________
> 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/
I don't really agree that there are multiple surprising answers here.  If you
iterate through the whole map, that will produce some number of elements, and
that's the length.  Whether you can calculate that number in __len__() depends
on the particular iterables you have, which is fine, but I don't think the
definition of length is ambiguous.

But I think Steven is right that you can't implement __len__() for an iterator
without running into some inconsistencies.  It's just unfortunate that map() is
an iterator.

-Kale



More information about the Python-ideas mailing list