yield

Gerrit Holl gerrit at nl.linux.org
Sat Jan 17 11:59:21 EST 2004


> i didnt understand the purpose of  'yield' keyword and the concept of 'generators' in python.  can someone explain me with a small example how  generators differ from normal function calls?
> kindly enlighten

You can see 'yield' as a continuable function:

def f():
    yield time.time()
    yield time.time()
    yield time.time()

now f() will return a generator, with a .next() method. Try:
g = f()
g.next()
g.next()
g.next()

on the interactive prompt, and see what happens...

yours,
Gerrit.

-- 
Mozilla _is_ the web: it grows faster than you can download it.
1011001 1101111 1110101 1110010 1110011 0101100
1000111 1100101 1110010 1110010 1101001 1110100




More information about the Python-list mailing list