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

David Mertz mertz at gnosis.cx
Sat Dec 1 12:06:23 EST 2018

On Sat, Dec 1, 2018, 11:54 AM Steven D'Aprano <steve at pearwood.info wrote:

> # current behaviour
> mo = map(lambda x: x, "aardvark")
> list(islice(mo, 3))  # discard the first three items
> assert ''.join(mo) == 'dvark'
> => passes
> # future behaviour, with your proposal
> assert ''.join(mo) == 'dvark'
> => fails with AssertionError
> Given the certainty that this change will break code (I know it will
> break *my* code, as I often rely on map() being an iterator not a
> sequence) it might be better to introduce a new "mapview" type rather than
> change the behaviour of map() itself. On the other hand, since the fix is
> simple enough:
> mo = iter(mo)

Given that the anti-fix is just as simple and currently available, I don't
see why we'd want a change:

# map->sequence
mo = list(mo)

FWIW, I actually do write exactly that code fairly often, it's not hard.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181201/2977088c/attachment.html>

More information about the Python-ideas mailing list