Understanding this generator function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hussein B

    Understanding this generator function

    Hey,
    This is an example of a generator function:
    =====
    def counter(start_a t=0):
    count = start_at
    while True:
    val = (yield count)
    if val is not None:
    count = val
    else:
    count += 1
    ======
    >>count = counter(5)
    >>count.next( )
    5
    >>count.send( 9)
    9
    ======
    I'm not able to understand how this generator function is working,
    would you please me (what happens when calling next/send)?
    Thanks.
  • Ben Finney

    #2
    Re: Understanding this generator function

    Hussein B <hubaghdadi@gma il.comwrites:
    This is an example of a generator function [using coroutine
    behaviour]:
    […]
    I'm not able to understand how this generator function is working,
    would you please me (what happens when calling next/send)?
    The behaviour you're seeing was introduced in PEP 342, "Coroutines via
    Enhanced Generators" <URL:http://www.python.org/dev/peps/pep-0342>,
    which documents the behaviour.

    Read that PEP, experiment further, and then ask any questions that you
    still have after that.

    --
    \ “Patience, n. A minor form of despair, disguised as a virtue.” |
    `\ —Ambrose Bierce, _The Devil's Dictionary_, 1906 |
    _o__) |
    Ben Finney

    Comment

    • James Mills

      #3
      Re: Understanding this generator function

      Hi,

      There is a great set of slides on this topic
      available here: http://www.dabeaz.com/generators/

      They explain this concept quite well and
      walk you through everything you need to
      know about generators and how powerful
      they can be.

      Please read it.

      cheers
      James

      On 8/27/08, Hussein B <hubaghdadi@gma il.comwrote:
      Hey,
      This is an example of a generator function:
      =====
      def counter(start_a t=0):
      count = start_at
      while True:
      val = (yield count)
      if val is not None:
      count = val
      else:
      count += 1
      ======
      >>count = counter(5)
      >>count.next( )
      5
      >>count.send( 9)
      9
      ======
      I'm not able to understand how this generator function is working,
      would you please me (what happens when calling next/send)?
      Thanks.
      >
      --

      >

      --
      --
      -- "Problems are solved by method"

      Comment

      • Jeff

        #4
        Re: Understanding this generator function

        def counter(start_a t=0):
            count = start_at
            while True:
                val = (yield count)
        A generator can accept a value from the consumer. So, If I have a
        counter:

        c = counter()

        I can send it a value:

        c.send(9)
                if val is not None:
                    count = val
        The generator tests to see it it received anything, and if it does,
        resets count to what it received (in this case, 9).
                else:
                    count += 1
        Otherwise, it just increments count on each call.

        Comment

        Working...