Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39974
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <vytasd2013@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none:': 0.05; '__name__': 0.07; 'assign': 0.07; 'main()': 0.07; 'val': 0.07; 'cc:addr:python-list': 0.10; 'def': 0.10; 'value.': 0.15; '"""returns': 0.16; '"none"': 0.16; "'__main__':": 0.16; 'count)': 0.16; 'counter()': 0.16; 'execute,': 0.16; 'folks,': 0.16; 'helps.': 0.16; 'main():': 0.16; 'return,': 0.16; 'statement.': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'yield': 0.17; 'feb': 0.19; 'trying': 0.21; 'object.': 0.22; 'resumes': 0.22; 'runs': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; '----': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'finds': 0.29; 'sleep': 0.29; 'statements': 0.29; 'url:mailman': 0.29; 'starts': 0.29; 'skip:& 10': 0.29; 'returned': 0.30; 'function': 0.30; 'code': 0.31; 'point': 0.31; 'url:python': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'goes': 0.33; 'function.': 0.33; 'hi,': 0.33; 'another': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'saved': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'gives': 0.39; 'called': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'more': 0.63; 'making': 0.64; '26,': 0.65; 'difficulty': 0.65; '2013': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=rIFyCwlEt2v1+Yyjool64tYB1JCIlqrv3vQ2FqJp6Vo=; b=ppSic7zHeZU/BROKzmoQjNl+edlBvtZPrwFwf60Q+Q50P3kmU889r5ACWrhP5IPeWL Esr5Jl/qsO7O2XXnmDpkzUkctSca+MFdENU3NIr5gCAYgtg1qGRhvVBeCaTmh0fZRh/A t6gUA8mMVZd47JwQliy5SGjTVNlXNXKgpZICsxAegXZ+u6wAb/azEjqlygPODyIBxieW Iw6lRSuWJZO7EmIEzbGvTy6xaQrLSgUuAfofJi5h1l6nlxsoo4c4fLieLXpjAyzR5zaJ NHTZ8vR5oFQCc2TvMiBvo49gBX0QajPEi5yJRjiEsUIagUxYAgquxEMrwGKk2DzG8w5T 0LGg== |
| MIME-Version | 1.0 |
| X-Received | by 10.180.78.35 with SMTP id y3mr20941954wiw.22.1361898434912; Tue, 26 Feb 2013 09:07:14 -0800 (PST) |
| In-Reply-To | <kgio69$5i7$1@theodyn.ncf.ca> |
| References | <d7760f61-08dd-4186-8844-5f8415773a49@googlegroups.com> <kgio69$5i7$1@theodyn.ncf.ca> |
| Date | Tue, 26 Feb 2013 17:07:14 +0000 |
| Subject | Re: yield expression |
| From | "Vytas D." <vytasd2013@gmail.com> |
| To | "Colin J. Williams" <cjw@ncf.ca> |
| Content-Type | multipart/alternative; boundary=f46d043c7b846f179b04d6a3b049 |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2556.1361898438.2939.python-list@python.org> (permalink) |
| Lines | 153 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1361898438 news.xs4all.nl 6847 [2001:888:2000:d::a6]:36001 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:39974 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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 <cjw@ncf.ca> 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.
>
>>
>>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
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