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

E. Madison Bray erik.m.bray at gmail.com
Wed Nov 28 10:04:33 EST 2018


On Wed, Nov 28, 2018 at 3:54 PM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Thu, Nov 29, 2018 at 1:46 AM Jonathan Fine <jfine2358 at gmail.com> wrote:
> >
> > On Wed, Nov 28, 2018 at 2:28 PM E. Madison Bray <erik.m.bray at gmail.com> wrote:
> >
> > > I mostly agree with the existing objections, though I have often found
> > > myself wanting this too, especially now that `map` does not simply
> > > return a list.  This problem alone (along with the same problem for
> > > filter) has had a ridiculously outsized impact on the Python 3 porting
> > > effort for SageMath, and I find it really irritating at times.
> >
> > I'm a mathematician, so understand your concerns. Here's what I hope
> > is a helpful suggestion.
> >
> > Create a module, say sage.itertools that contains (not tested)
> >
> >    def py2map(iterable):
> >         return list(map(iterable))
>
> With the nitpick that the arguments should be (func, *iterables)
> rather than just the single iterable, yes, this is a viable transition
> strategy. In fact, it's very similar to what 2to3 would do, except
> that 2to3 would do it at the call site. If any Py3 porting process is
> being held up significantly by this, I would strongly recommend giving
> 2to3 an eyeball - run it on some of your code, then either accept its
> changes or just learn from the diffs. It's not perfect (nothing is),
> but it's a useful tool.

That effort is already mostly done and adding a helper function would
not have worked as users *passing* map(...) as an argument to some
function just expect it to work.  The only alternative would have been
replacing the builtin map with something else at the globals level.
2to3 is mostly useless since a major portion of Sage is written in
Cython anyways.

I just mentioned that porting effort for background.  I still believe
that the actual proposal of making the arguments to a map(...) call
accessible from Python as attributes of the map object (ditto filter,
zip, etc.) is useful in its own right, rather than just having this
completely opaque iterator.


More information about the Python-ideas mailing list