On Thu, Nov 29, 2018 at 08:13:12PM -0500, Paul Svensson wrote:

> Excellent proposal, followed by a flood of confused replies,
> which I will mostly disregard, since all miss the obvious.

When everyone around you is making technical responses which you think 
are "confused", it is wise to consider the possibility that it is you 
who is missing something rather than everyone else.


> 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?

No matter which choice you make, you're going to surprise and annoy 
people, and there will be circumstances where that choice will introduce 
bugs into their code.


> That implies, loosely speaking:
>  * map(f, Iterable) -> Iterable, and
>  * map(f, Sequence) -> Sequence

But map objects aren't sequences. They're iterators. Just adding a 
__len__ method isn't going to make them sequences (not even "loosely 
speaking") or solve the problem above.

In principle, we could make this work, by turning the output of map() 
into a view like dict.keys() etc, or a lazy sequence type like range(). 
wrapping the underlying sequence. That might be worth exploring. I can't 
think of any obvious problems with a view-like interface, but that 
doesn't mean there aren't any. I've spent like 30 seconds thinking about 
it, so the fact that I can't see any problems with it means little.

But its also a big change, not just a matter of exposing the __len__ 
method of the underlying iterable (or iterables).


-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to