yield keyword usage

Discussion in 'Python' started by Ehsan, Jul 30, 2007.

  1. Ehsan

    Ehsan Guest

    hi
    coulde any one show me the usage of "yield" keyword specially in this
    example:


    """Fibonacci sequences using generators

    This program is part of "Dive Into Python", a free Python book for
    experienced programmers. Visit http://diveintopython.org/ for the
    latest version.
    """

    __author__ = "Mark Pilgrim ()"
    __version__ = "$Revision: 1.2 $"
    __date__ = "$Date: 2004/05/05 21:57:19 $"
    __copyright__ = "Copyright (c) 2004 Mark Pilgrim"
    __license__ = "Python"

    def fibonacci(max):
    a, b = 0, 1
    while a < max:
    yield a
    a, b = b, a+b

    for n in fibonacci(1000):
    print n,
     
    Ehsan, Jul 30, 2007
    #1
    1. Advertising

  2. Ehsan

    Erik Jones Guest

    On Jul 30, 2007, at 4:13 PM, Ehsan wrote:

    > hi
    > coulde any one show me the usage of "yield" keyword specially in this
    > example:
    >
    >
    > """Fibonacci sequences using generators
    >
    > This program is part of "Dive Into Python", a free Python book for
    > experienced programmers. Visit http://diveintopython.org/ for the
    > latest version.
    > """
    >
    > __author__ = "Mark Pilgrim ()"
    > __version__ = "$Revision: 1.2 $"
    > __date__ = "$Date: 2004/05/05 21:57:19 $"
    > __copyright__ = "Copyright (c) 2004 Mark Pilgrim"
    > __license__ = "Python"
    >
    > def fibonacci(max):
    > a, b = 0, 1
    > while a < max:
    > yield a
    > a, b = b, a+b
    >
    > for n in fibonacci(1000):
    > print n,


    As in how it works? Sure, when you use the yield statement in a
    function or method you turn it into a generator method that can then
    be used for iteration. What happens is that when fibonacci(1000) is
    called in the for loop statement it executes up to the yield
    statement where it "yields" the then current value of a to the
    calling context at which point n in the for loop is bound to that
    value and the for loop executes one iteration. Upon the beginning of
    the for loop's next iteration the fibonacci function continues
    execution from the yield statment until it either reaches another
    yield statement or ends. In this case, since the yield occured in a
    loop, it will be the same yield statement at which point it will
    "yield" the new value of a. It should be obvious now that this whole
    process will repeat until the condition a < max is not longer true in
    the fibonacci function at which point the function will return
    without yielding a value and the main loop (for n in ...) will
    terminate.



    Erik Jones

    Software Developer | Emma®

    800.595.4401 or 615.292.5888
    615.292.0777 (fax)

    Emma helps organizations everywhere communicate & market in style.
    Visit us online at http://www.myemma.com
     
    Erik Jones, Jul 30, 2007
    #2
    1. Advertising

  3. Ehsan

    Guest

    On Jul 30, 4:40 pm, Erik Jones <> wrote:
    > On Jul 30, 2007, at 4:13 PM, Ehsan wrote:
    >
    >
    >
    >
    >
    > > hi
    > > coulde any one show me the usage of "yield" keyword specially in this
    > > example:

    >
    > > """Fibonacci sequences using generators

    >
    > > This program is part of "Dive Into Python", a free Python book for
    > > experienced programmers. Visithttp://diveintopython.org/for the
    > > latest version.
    > > """

    >
    > > __author__ = "Mark Pilgrim ()"
    > > __version__ = "$Revision: 1.2 $"
    > > __date__ = "$Date: 2004/05/05 21:57:19 $"
    > > __copyright__ = "Copyright (c) 2004 Mark Pilgrim"
    > > __license__ = "Python"

    >
    > > def fibonacci(max):
    > > a, b = 0, 1
    > > while a < max:
    > > yield a
    > > a, b = b, a+b

    >
    > > for n in fibonacci(1000):
    > > print n,

    >
    > As in how it works? Sure, when you use the yield statement in a
    > function or method you turn it into a generator method that can then
    > be used for iteration. What happens is that when fibonacci(1000) is
    > called in the for loop statement it executes up to the yield
    > statement where it "yields" the then current value of a to the
    > calling context at which point n in the for loop is bound to that
    > value and the for loop executes one iteration. Upon the beginning of
    > the for loop's next iteration the fibonacci function continues
    > execution from the yield statment until it either reaches another
    > yield statement or ends. In this case, since the yield occured in a
    > loop, it will be the same yield statement at which point it will
    > "yield" the new value of a. It should be obvious now that this whole
    > process will repeat until the condition a < max is not longer true in
    > the fibonacci function at which point the function will return
    > without yielding a value and the main loop (for n in ...) will
    > terminate.


    Also note that the function could terminate without ever executing
    a yield statement (if max<0). In which case nothing would get printed
    just like in

    for n in []: print n,

    You could also force the generator to abort rather than relying
    on the while loop not executing by using a return instead of a yield
    statement. This can be useful if the while executes even when given
    bad arguments.

    def fibonacci(max):
    if max < 0: # test for invalid argument
    print 'max must be >0' # diagnostic message
    return # kill generator
    a, b = 0, 1
    while a < max:
    yield a
    a, b = b, a+b

    for n in fibonacci(1000):
    print n,

    print
    print

    for n in fibonacci(-1000):
    print n,


    ## 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
    ##
    ## max must be >0


    >
    > Erik Jones
    >
    > Software Developer | Emma®
    >
    > 800.595.4401 or 615.292.5888
    > 615.292.0777 (fax)
    >
    > Emma helps organizations everywhere communicate & market in style.
    > Visit us online athttp://www.myemma.com
     
    , Jul 30, 2007
    #3
  4. Ehsan

    Steve Holden Guest

    Ehsan wrote:
    > hi
    > coulde any one show me the usage of "yield" keyword specially in this
    > example:
    >
    >
    > """Fibonacci sequences using generators
    >
    > This program is part of "Dive Into Python", a free Python book for
    > experienced programmers. Visit http://diveintopython.org/ for the
    > latest version.
    > """
    >
    > __author__ = "Mark Pilgrim ()"
    > __version__ = "$Revision: 1.2 $"
    > __date__ = "$Date: 2004/05/05 21:57:19 $"
    > __copyright__ = "Copyright (c) 2004 Mark Pilgrim"
    > __license__ = "Python"
    >
    > def fibonacci(max):
    > a, b = 0, 1
    > while a < max:
    > yield a
    > a, b = b, a+b
    >
    > for n in fibonacci(1000):
    > print n,
    >

    A function body with at least one yield expression (formerly a yield
    statement) in its body returns a generator when called:

    >>> def gen(i):

    .... for k in range(i):
    .... yield k*k
    ....
    >>> g = gen(3)
    >>> g

    <generator object at 0x7ff2822c>
    >>>


    The same function can be called multiple times to create independent
    generators - the particular example I have given created a generator for
    a given number of squares.

    The generator can be used in an iterative context (such as a for loop),
    but in actual fact it observes the Python iterator protocol, so the
    generator has a .next() method, which can be used to extract the next
    value from it. Calling the .next() method runs the generator until a
    yield expression is encountered, and the value of the yield expression
    is the return value of the next method:

    >>> g.next()

    0
    >>> g.next()

    1
    >>> g.next()

    4

    When the generator function terminates (either with a return statement
    or by dropping off the end) it raises a StopIteration exception. This
    will terminate an iteration, but in other contexts it can be handled
    just like any other exception:

    >>> g.next()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration
    >>>


    So the fibonacci example you quote is a generator function that will
    yield a sequence of Fibonacci numbers up to but not including the value
    of its argument.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Jul 31, 2007
    #4
    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. Replies:
    1
    Views:
    322
    Gabriel Genellina
    Apr 22, 2008
  2. Peng Yu

    the usage of 'yield' keyword

    Peng Yu, Oct 14, 2009, in forum: Python
    Replies:
    3
    Views:
    327
    Sean DiZazzo
    Oct 16, 2009
  3. Markus
    Replies:
    1
    Views:
    199
    Mark Hubbart
    Sep 27, 2004
  4. Michael Edgar
    Replies:
    13
    Views:
    276
    Brian Candler
    Apr 21, 2011
  5. VK
    Replies:
    1
    Views:
    85
    Thomas 'PointedEars' Lahn
    Nov 29, 2007
Loading...

Share This Page