is_iterable function.

George Sakkis george.sakkis at gmail.com
Wed Jul 25 16:02:14 EDT 2007


On Jul 25, 3:26 pm, Neil Cerutti <horp... at yahoo.com> wrote:

> Speaking of the iter builtin function, is there an example of the
> use of the optional sentinel object somewhere I could see?

# iterate over random numbers from 1 to 10; use 0 as a sentinel to
stop the iteration
for n in iter(lambda:random.randrange(10), 0):
    print n

More generally, iter(callable, sentinel) is just a convenience
function for the following generator:

def iter(callable, sentinel):
    while True:
        c = callable()
        if c == sentinel: break
        yield c


George




More information about the Python-list mailing list