On Wed, Nov 28, 2018 at 4:04 PM Steven D'Aprano <[email protected]> wrote:
>
> On Wed, Nov 28, 2018 at 03:27:25PM +0100, E. Madison Bray 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.
> >
> > As a simple counter-proposal which I believe has fewer issues, I would
> > really like it if the built-in `map()` and `filter()` at least
> > provided a Python-level attribute to access the underlying iterables.
> > This is necessary because if I have a function that used to take, say,
> > a list as an argument, and it receives a `map` object, I now have to
> > be able to deal with map()s, and I may have checks I want to perform
> > on the underlying iterables before, say, I try to iterate over the
> > `map`.
> >
> > Exactly what those checks are and whether or not they're useful may be
> > highly application-specific, which is why say a generic `map.__len__`
> > is not workable.  However, if I can at least inspect those iterables I
> > can make my own choices on how to handle the map.
>
> Can you give a concrete example of what you would do in practice? I'm
> having trouble thinking of how and when this sort of thing would be
> useful. Aside from extracting the length of the iterable(s), under what
> circumstances would you want to bypass the call to map() or filter() and
> access the iterables directly?

For example, some function that used to expect some finite-sized
sequence such as a list or tuple is now passed a "map", possibly
wrapping one or more iterable of arbitrary, possibly non-finite size.
For the purposes of some algorithm I have this is not useful and I
need to convert it to a sequence anyways but don't want to do that
without some guarantee that I won't blow up the user's memory usage.
So I might want to check:

finite_definite = True
for it in my_map.iters:
    try:
        len(it)
    except TypeError:
        finite_definite = False

if finite_definite:
    my_seq = list(my_map)
else:
    # some other algorithm

Of course, some arbitrary object could lie about its __len__ but I'm
not concerned about pathological cases here.  There may be other
opportunities for optimization as well that are otherwise hidden.

Either way, I don't see any reason to hide this data; it's a couple of
slot attributes and instantly better introspection capability.
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to