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
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