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

E. Madison Bray erik.m.bray at gmail.com
Thu Nov 29 08:16:48 EST 2018


On Thu, Nov 29, 2018 at 1:38 PM Steven D'Aprano <steve at pearwood.info> wrote:
>
> On Thu, Nov 29, 2018 at 12:16:37PM +0100, E. Madison Bray wrote:
> > On Wed, Nov 28, 2018 at 11:27 PM Steven D'Aprano <steve at pearwood.info> wrote:
>
> ["it" below being the length of an arbitrary iterator]
>
> > > If we could solve it, that would be great -- but I'm not convinced that
> > > it is solvable, since the solution seems worse than the problem it aims
> > > to solve. But I live in hope that somebody cleverer than me can point
> > > out the flaws in my argument.
> >
> > In general it's unsolvable, so no attempt should be made to provide a
> > pre-baked attempt at a solution that won't always work.  But in many,
> > if not the majority of cases, it *is* solvable.  So let's give
> > intelligent people the tools they need to solve it in those cases that
> > they know they can solve it :)
>
> So you say, but the solutions made so far seem fatally flawed to me.
>
> Just repeating the assertion that it is solvable isn't very convincing.

Okay, let's keep it simple:

m = map(str, [1, 2, 3])
len_of_m = None
if len(m.iters) == 1 and isinstance(m.iters[0], Sized):
    len_of_m = len(m.iters[0])

You can give me pathological cases where that isn't true, but you
can't say there's no context in which that wouldn't be virtually
guaranteed and consenting adults can decide whether or not that's a
safe-enough assumption in their own code.


More information about the Python-ideas mailing list