Re: yield

Discussion in 'Python' started by Gerrit Holl, Jan 17, 2004.

  1. Gerrit Holl

    Gerrit Holl Guest

    > 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
     
    Gerrit Holl, Jan 17, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    2
    Views:
    2,517
    Roedy Green
    Oct 4, 2003
  2. qjzhu
    Replies:
    3
    Views:
    23,089
    Roedy Green
    Oct 13, 2003
  3. Replies:
    1
    Views:
    341
    Gabriel Genellina
    Apr 22, 2008
  4. Markus
    Replies:
    1
    Views:
    217
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    303
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page