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

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Nov 28 15:41:41 EST 2018


E. Madison Bray wrote:
> 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`.

This sounds like a backwards way to address the issue. If you
have a function that expects a list in particular, it's up to
its callers to make sure they give it one. Instead of maing the
function do a bunch of looking before it leaps, it would be
better to define something like

    def lmap(f, *args): return list(map(f, *args))

and then replace 'map' with 'lmap' elsewhere in your code.

-- 
Greg


More information about the Python-ideas mailing list