dict generator question

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Sep 19 21:51:05 EDT 2008


On Fri, 19 Sep 2008 17:00:56 -0700, MRAB wrote:

> Extending len() to support iterables sounds like a good idea, except
> that it could be misleading when:
> 
>     len(file(path))
> 
> returns the number of lines and /not/ the length in bytes as you might
> first think!

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?



-- 
Steven



More information about the Python-list mailing list