Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39973
| Date | 2013-02-26 12:05 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: yield expression |
| References | <d7760f61-08dd-4186-8844-5f8415773a49@googlegroups.com> <kgio69$5i7$1@theodyn.ncf.ca> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2555.1361898338.2939.python-list@python.org> (permalink) |
On 02/26/2013 11:34 AM, Colin J. Williams wrote:
> 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.
>>
>
'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
--
DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
yield expression Ziliang Chen <zlchen.ken@gmail.com> - 2013-02-24 16:36 -0800
Re: yield expression "Colin J. Williams" <cjw@ncf.ca> - 2013-02-26 11:34 -0500
Re: yield expression Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-26 09:50 -0700
Re: yield expression Dave Angel <davea@davea.name> - 2013-02-26 12:05 -0500
Re: yield expression "Vytas D." <vytasd2013@gmail.com> - 2013-02-26 17:07 +0000
Re: yield expression "Colin J. Williams" <cjw@ncf.ca> - 2013-02-26 13:44 -0500
Re: yield expression Dave Angel <davea@davea.name> - 2013-02-26 21:59 -0500
Re: yield expression "Colin J. Williams" <cjw@ncf.ca> - 2013-02-26 13:44 -0500
csiph-web