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: 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: References: Date: Tue, 26 Feb 2013 17:07:14 +0000 Subject: Re: yield expression From: "Vytas D." To: "Colin J. Williams" 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --f46d043c7b846f179b04d6a3b049 Content-Type: text/plain; charset=ISO-8859-1 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 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 > --f46d043c7b846f179b04d6a3b049 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi,

You are using "yield" incorrectl= y. "yield" works like return, but it can return more than once fr= om 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 functio= n and goes until it finds "yield" and returns the value. The stat= e of generator is saved - it is like it is put to sleep until you call it a= gain. So the next time you call generator() it runs from the point it retur= ned 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=3D0):=A0=A0=A0 """Returns integer each time called""&q= uot;

=A0=A0=A0 count =3D start_at
=A0=A0=A0 while True:
=A0=A0=A0=A0= =A0=A0=A0 yield count
=A0=A0=A0=A0=A0=A0=A0 count +=3D 1
=A0=A0=A0 def main():
=A0=A0=A0 generator =3D counter()

=A0=A0=A0 while T= rue:
=A0=A0=A0=A0=A0=A0=A0 print(next(generator))


if __name__= =3D=3D '__main__':
=A0=A0=A0 main()


Hope helps.

Vytas = D.



On Tue, Feb 26, 2013 at 4:34 PM, Colin J. Williams <cjw@ncf.c= a> 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&qu= ot; will be "None" ? What's happening under the hood? It seem= s to me very time the counter resumes to execute, it will assign "coun= t" to "val", so "val" should NOT be "None&quo= t; all the time.

Thanks !

code snippet:
----
=A0 def counter(start_at=3D0):
=A0 =A0 =A0 count =3D start_at
=A0 =A0 =A0 while True:
=A0 =A0 =A0 =A0 =A0 val =3D (yield count)
=A0 =A0 =A0 =A0 =A0 if val is not None:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 count =3D val
=A0 =A0 =A0 =A0 =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 print 'val is None'
=A0 =A0 =A0 =A0 =A0 =A0 =A0 count +=3D 1

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

Colin W.


--
http://mail.python.org/mailman/listinfo/python-list

--f46d043c7b846f179b04d6a3b049--