Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39833 > unrolled thread
| Started by | Ziliang Chen <zlchen.ken@gmail.com> |
|---|---|
| First post | 2013-02-24 16:39 -0800 |
| Last post | 2013-02-24 16:59 -0800 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.python
yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:39 -0800
Re: yield expression Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-25 00:51 +0000
Re: yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:59 -0800
Re: yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:59 -0800
| From | Ziliang Chen <zlchen.ken@gmail.com> |
|---|---|
| Date | 2013-02-24 16:39 -0800 |
| Subject | yield expression |
| Message-ID | <eb6313dd-94f9-406d-b353-3d64d171e0a7@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-02-25 00:51 +0000 |
| Message-ID | <mailman.2469.1361753515.2939.python-list@python.org> |
| In reply to | #39833 |
On 25 February 2013 00:39, Ziliang Chen <zlchen.ken@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Ziliang Chen <zlchen.ken@gmail.com> |
|---|---|
| Date | 2013-02-24 16:59 -0800 |
| Message-ID | <8012f480-7dc8-4a30-b3e8-8fdeebf2791e@googlegroups.com> |
| In reply to | #39840 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Ziliang Chen <zlchen.ken@gmail.com> |
|---|---|
| Date | 2013-02-24 16:59 -0800 |
| Message-ID | <mailman.2470.1361753950.2939.python-list@python.org> |
| In reply to | #39840 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web