[Python-ideas] Suggested MapView object (Re: __len__() for map())

Chris Barker chris.barker at noaa.gov
Mon Dec 10 20:15:36 EST 2018

On Mon, Dec 10, 2018 at 5:23 AM E. Madison Bray <erik.m.bray at gmail.com>

> Indeed; I believe it is very useful to have a map-like object that is
> effectively an augmented list/sequence.

but what IS a "map-like object" -- I'm trying to imagine what that actually

"map" takes a function and maps it onto a interable, returning a new
iterable. So a map object is an iterable -- what's under the hood being
used to create it is (and should remain) opaque.

Back in the day, Python was "all about sequences" -- so map() took a
sequence and returned a sequence (an actual list, but that's not the point
here). And that's pretty classic "map".

With py3, there was a big shift toward iterables, rather than sequences as
the core type to work with. There are a few other benefits, but the main
one is that often sequences were made, simply so that they could be
immediately iterated over, and that was a waste of resources.

for i, item in enumerate(a_sequence):

for x, y in zip(seq1, seq2):

These two are pretty obvious, but the same approach was taken over much of
python: dict.keys(), map(), range(), ....

So now in Python, you need to decide, when writing code, what your API is
-- does your function take a sequence? or does it take an iterable?

Of course, a sequence is an iterable, but a iterable is not (necessarily) a
sequence. -- so back in the day, you din't really need to make the decision.

So in the case of the Sage example -- I wonder what the real problem is --
if you have an API that requires a sequence, on Py2, folks may have well
been passing it the result of a map() call. -- note that they weren't
passing a "map object" that is now somehow different than it used to be --
they were passing a list plain and simple. And there are all sorts of
places, when converting from py2 to py3, where you will now get an iterable
that isn't a proper sequence, and if the API you are using requires a
sequence, you need to wrap a list() or tuple() or some such around it to
make the sequence.

Note that you can write your code to work under either 2 or 3, but it's
really hard to write a library so that your users can run it under either 2
or 3 without any change in their code!

But note: the fact that it's a map object is just one special case.

I suppose one could write an API now that actually expects a map object
(rather than a generic sequence or iterable) but it was literally
impossible in py2 -- there was no such object.

I'm still confused -- what's so wrong with:

list(map(func, some_iterable))

if you need a sequence?

You can, of course mike lazy-evaluated sequences (like range), and so you
could make a map-like function that required a sequence as input, and would
lazy evaluate that sequence. This could be useful if you weren't going to
work with the entire collection, but really wanted to only index out a few
items, but I'm trying to imagine a use case for that, and I haven't. And I
don't think that's the use case that started this thread...


> _______________________________________________
> 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/


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181210/67cdc025/attachment-0001.html>

More information about the Python-ideas mailing list