dict generator question

Miles semanticist at gmail.com
Mon Sep 22 08:53:16 CEST 2008


On Fri, Sep 19, 2008 at 9:51 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> Extending len() to support iterables sounds like a good idea, except that
> it's not.
>
> Here are two iterables:
>
>
> def yes():  # like the Unix yes command
>    while True:
>        yield "y"
>
> def rand(total):
>    "Return random numbers up to a given total."
>    from random import random
>    tot = 0.0
>    while tot < total:
>        x = random()
>        yield x
>        tot += x
>
>
> What should len(yes()) and len(rand(100)) return?

Clearly, len(yes()) would never return, and len(rand(100)) would
return a random integer not less than 101.

-Miles



More information about the Python-list mailing list