yield expression

Discussion in 'Python' started by Ziliang Chen, Feb 25, 2013.

  1. Ziliang Chen

    Ziliang Chen Guest

    Hi folks,
    When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ?What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time.

    Thanks !

    code snippet:
    ----
    def counter(start_at=0):
    count = start_at
    while True:
    val = (yield count)
    if val is not None:
    count = val
    else:
    print 'val is None'
    count += 1
    Ziliang Chen, Feb 25, 2013
    #1
    1. Advertising

  2. On 25 February 2013 00:39, Ziliang Chen <> wrote:
    > Hi folks,
    > When I am trying to understand "yield" expression in Python2.6, I did thefollowing coding. I have difficulty understanding why "val" will be "None"? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time.
    >
    > Thanks !
    >
    > code snippet:
    > ----
    > def counter(start_at=0):
    > count = start_at
    > while True:
    > val = (yield count)
    > if val is not None:
    > count = val
    > else:
    > print 'val is None'
    > count += 1


    The value of the yield expression is usually None. yield only returns
    a value if the caller of a generator function sends one with the send
    method (this is not commonly used). The send method supplies a value
    to return from the yield expression and then returns the value yielded
    by the next yield expression. For example:

    >>> g = counter()
    >>> next(g) # Need to call next() once to suspend at the first yield call

    0
    >>> g.send('value for count') # Now we can send a value for yield to return

    'value for count'


    Oscar
    Oscar Benjamin, Feb 25, 2013
    #2
    1. Advertising

  3. Ziliang Chen

    Ziliang Chen Guest

    On Monday, February 25, 2013 8:51:28 AM UTC+8, Oscar Benjamin wrote:
    > On 25 February 2013 00:39, Ziliang Chen wrote:
    >
    > > Hi folks,

    >
    > > When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be"None" all the time.

    >
    > >

    >
    > > Thanks !

    >
    > >

    >
    > > code snippet:

    >
    > > ----

    >
    > > def counter(start_at=0):

    >
    > > count = start_at

    >
    > > while True:

    >
    > > val = (yield count)

    >
    > > if val is not None:

    >
    > > count = val

    >
    > > else:

    >
    > > print 'val is None'

    >
    > > count += 1

    >
    >
    >
    > The value of the yield expression is usually None. yield only returns
    >
    > a value if the caller of a generator function sends one with the send
    >
    > method (this is not commonly used). The send method supplies a value
    >
    > to return from the yield expression and then returns the value yielded
    >
    > by the next yield expression. For example:
    >
    >
    >
    > >>> g = counter()

    >
    > >>> next(g) # Need to call next() once to suspend at the first yield call

    >
    > 0
    >
    > >>> g.send('value for count') # Now we can send a value for yield to return

    >
    > 'value for count'
    >
    >
    >
    >
    >
    > Oscar


    Thanks Oscar !
    I am cleared. Only when "send" is used to feed "yield" a new value, the "yield" expression has none "None", otherwise, "yield" expression has "None" value.
    Ziliang Chen, Feb 25, 2013
    #3
  4. Ziliang Chen

    Ziliang Chen Guest

    On Monday, February 25, 2013 8:51:28 AM UTC+8, Oscar Benjamin wrote:
    > On 25 February 2013 00:39, Ziliang Chen wrote:
    >
    > > Hi folks,

    >
    > > When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be"None" all the time.

    >
    > >

    >
    > > Thanks !

    >
    > >

    >
    > > code snippet:

    >
    > > ----

    >
    > > def counter(start_at=0):

    >
    > > count = start_at

    >
    > > while True:

    >
    > > val = (yield count)

    >
    > > if val is not None:

    >
    > > count = val

    >
    > > else:

    >
    > > print 'val is None'

    >
    > > count += 1

    >
    >
    >
    > The value of the yield expression is usually None. yield only returns
    >
    > a value if the caller of a generator function sends one with the send
    >
    > method (this is not commonly used). The send method supplies a value
    >
    > to return from the yield expression and then returns the value yielded
    >
    > by the next yield expression. For example:
    >
    >
    >
    > >>> g = counter()

    >
    > >>> next(g) # Need to call next() once to suspend at the first yield call

    >
    > 0
    >
    > >>> g.send('value for count') # Now we can send a value for yield to return

    >
    > 'value for count'
    >
    >
    >
    >
    >
    > Oscar


    Thanks Oscar !
    I am cleared. Only when "send" is used to feed "yield" a new value, the "yield" expression has none "None", otherwise, "yield" expression has "None" value.
    Ziliang Chen, Feb 25, 2013
    #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:
    314
    Gabriel Genellina
    Apr 22, 2008
  2. Markus
    Replies:
    1
    Views:
    185
    Mark Hubbart
    Sep 27, 2004
  3. Michael Edgar
    Replies:
    13
    Views:
    257
    Brian Candler
    Apr 21, 2011
  4. Ziliang Chen

    yield expression

    Ziliang Chen, Feb 25, 2013, in forum: Python
    Replies:
    7
    Views:
    113
    Dave Angel
    Feb 27, 2013
  5. Ziliang Chen

    yield expression

    Ziliang Chen, Feb 25, 2013, in forum: Python
    Replies:
    0
    Views:
    74
    Ziliang Chen
    Feb 25, 2013
Loading...

Share This Page