[Python-ideas] Suggested MapView object (Re: __len__() for map())
tjreedy at udel.edu
Tue Dec 11 14:08:47 EST 2018
On 12/11/2018 12:01 PM, Chris Barker - NOAA Federal via Python-ideas wrote:
> Perhaps I got confused by the early part of this discussion.
> My point was that there is no “map-like” object at the Python level.
> (That is no Map abc).
> Py2’s map produced a sequence. Py3’s map produced an iterable.
> So any API that was expecting a sequence could accept the result of a
> py2 map, but not a py3 map. There is absolutely nothing special about
> map here.
> The example of range has been brought up, but I don’t think it’s
> analogous — py2 range returns a list, py3 range returns an immutable
> sequence. Because that’s as close as we can get to a sequence while
> preserving the lazy evaluation that is wanted.
> I _think_ someone may be advocating that map() could return an
> iterable if it is passed a iterable,
I believe you mean 'iterator' rather than 'iterable' here and below as a
sequence is an iterable.
> and a sequence of it is passed a sequence.
> Yes, it could, but that seems like a bad idea to me.
> But folks are proposing a “map” that would produce a lazy-evaluated
> sequence. Sure — as Paul said, put it up on pypi and see if folks find
> it useful.
> Personally, I’m still finding it hard to imagine a use case where you
> need the sequence features, but also lazy evaluation is important.
> Sure: range() has that, but it came at almost zero cost, and I’m not
> sure the sequence features are used much.
> Note: the one use-case I can think of for a lazy evaluated sequence
> instead of an iterable is so that I can pick a random element with
> random.choice(). (Try to pick a random item from. a dict), but that
> doesn’t apply here—pick a random item from the source sequence
> But this is specific example of a general use case: you need to access
> only a subset of the mapped sequence (or access it out of order) so
> using the iterable version won’t work, and it may be large enough that
> making a new sequence is too resource intensive.
> Seems rare to me, and in many cases, you could do the subsetting
> before applying the function, so I think it’s a pretty rare use case.
> But go ahead and make it — I’ve been wrong before :-)
More information about the Python-ideas