That would be great especially if it returned objects of a subclass of map
so that it didn't break any code that checks isinstance, however; I think
this goes a little beyond map. I've run into cases using itertools where I
wished the iterators could support len. I suppose you could turn those all
into factories too, but I wonder if that's the most elegant solution.


On Thu, Nov 29, 2018 at 7:22 PM Paul Svensson <paul-pyt...@svensson.org>
wrote:

> On Mon, Nov 26, 2018 at 10:35 PM Kale Kundert <k...@thekunderts.net>
> wrote:
> >
> > I just ran into the following behavior, and found it surprising:
> >
> >>>> len(map(float, [1,2,3]))
> > TypeError: object of type 'map' has no len()
> >
> > I understand that map() could be given an infinite sequence and
> therefore might not always have a length.  But in this case, it seems like
> map() should've known that its length was 3.  I also understand that I can
> just call list() on the whole thing and get a list, but the nice thing
> about map() is that it doesn't copy data, so it's unfortunate to lose that
> advantage for no particular reason.
> >
> > My proposal is to delegate map.__len__() to the underlying iterable.
> Similarly, map.__getitem__() could be implemented if the underlying
> iterable supports item access:
> >
>
> Excellent proposal, followed by a flood of confused replies,
> which I will mostly disregard, since all miss the obvious.
>
> What's being proposed is simple, either:
>   * len(map(f, x)) == len(x), or
>   * both raise TypeError
>
> That implies, loosely speaking:
>   * map(f, Iterable) -> Iterable, and
>   * map(f, Sequence) -> Sequence
>
> But, *not*:
>   * map(f, Iterable|Sequence) -> Magic.
>
> So, the map() function becomes a factory, returning an object
> with __len__ or without, depending on what it was called with.
>
>         /Paul
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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