yield expression

Z

Ziliang Chen

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
 
C

Colin J. Williams

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

Perhaps it's becaoue (teild count) is a statement. Statements do not
return a value.

Colin W.
 
D

Dave Angel

Perhaps it's becaoue (teild count) is a statement. Statements do not
return a value.

Colin W.

'yield count' is a yield_expression, not always a statement. If it were
the first thing in a statement, it'd be a yield_stmt


See the docs: http://docs.python.org/2/reference/simple_stmts.html

assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression)

and http://docs.python.org/2/reference/expressions.html

yield_atom ::= "(" yield_expression ")"
yield_expression ::= "yield" [expression_list]

The value produced by the yield expression is produced by a.send()
method. This allows an approximation to coroutines.

I believe this dual usage of yield started in Python 2.5
 
V

Vytas D.

Hi,

You are using "yield" incorrectly. "yield" works like return, but it can
return more than once from the same function. Functions that "yield"
produce a so called "generator" object. This generator object gives you
values every time you call it.

The generator works very interesting way. It starts like normal function
and goes until it finds "yield" and returns the value. The state of
generator is saved - it is like it is put to sleep until you call it again.
So the next time you call generator() it runs from the point it returned
last time and will return you another value.

Simple sample of making and using generator (prints forever, so just kill
with CTRL+C).

def counter(start_at=0):
"""Returns integer each time called"""

count = start_at
while True:
yield count
count += 1

def main():
generator = counter()

while True:
print(next(generator))


if __name__ == '__main__':
main()


Hope helps.

Vytas D.
 
C

Colin J. Williams

Hi,

You are using "yield" incorrectly. "yield" works like return, but it can
return more than once from the same function. Functions that "yield"
produce a so called "generator" object. This generator object gives you
values every time you call it.

The generator works very interesting way. It starts like normal function
and goes until it finds "yield" and returns the value. The state of
generator is saved - it is like it is put to sleep until you call it
again. So the next time you call generator() it runs from the point it
returned last time and will return you another value.

Simple sample of making and using generator (prints forever, so just
kill with CTRL+C).

def counter(start_at=0):
"""Returns integer each time called"""

count = start_at
while True:
yield count
count += 1

def main():
generator = counter()

while True:
print(next(generator))


if __name__ == '__main__':
main()


Hope helps.

Vytas D.



On Tue, Feb 26, 2013 at 4:34 PM, Colin J. Williams <[email protected]

On 24/02/2013 7:36 PM, 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


Perhaps it's becaoue (teild count) is a statement. Statements do
not return a value.

Colin W.
Yes, it's very helpful. Thanks also to the other two responders.

This brings us back to the OP question. Why not " val = (yield count)"?

Colin W.
 
C

Colin J. Williams

Hi,

You are using "yield" incorrectly. "yield" works like return, but it can
return more than once from the same function. Functions that "yield"
produce a so called "generator" object. This generator object gives you
values every time you call it.

The generator works very interesting way. It starts like normal function
and goes until it finds "yield" and returns the value. The state of
generator is saved - it is like it is put to sleep until you call it
again. So the next time you call generator() it runs from the point it
returned last time and will return you another value.

Simple sample of making and using generator (prints forever, so just
kill with CTRL+C).

def counter(start_at=0):
"""Returns integer each time called"""

count = start_at
while True:
yield count
count += 1

def main():
generator = counter()

while True:
print(next(generator))


if __name__ == '__main__':
main()


Hope helps.

Vytas D.



On Tue, Feb 26, 2013 at 4:34 PM, Colin J. Williams <[email protected]

On 24/02/2013 7:36 PM, 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


Perhaps it's becaoue (teild count) is a statement. Statements do
not return a value.

Colin W.
Yes, it's very helpful. Thanks also to the other two responders.

This brings us back to the OP question. Why not " val = (yield count)"?

Colin W.
 
D

Dave Angel

Yes, it's very helpful. Thanks also to the other two responders.

This brings us back to the OP question. Why not " val = (yield count)"?

Colin W.

Repeating a misconception doesn't make it any more true. yield is both
an expression and a statement. Nothing wrong with the statement
val = (yield count)

although he apparently wasn't making any good use of the possible return
value, since he observed it was always None. That's just because he
didn't use the send method in the calling function.

Most links I see on a web search explain only the expression form of
yield, which is why i took a lot of time building the response I did
earlier.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top