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

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Nov 28 17:59:27 EST 2018


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

If map is being passed into your function, you can still do this
check before calling map.

If the user is doing the mapping themselves, then in Python 2 it
would have blown up anyway before your function even got called,
so nothing is any worse.

-- 
Greg




More information about the Python-ideas mailing list