Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.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; 'example:': 0.03; 'value,': 0.03; 'none:': 0.05; 'assign': 0.07; 'caller': 0.07; 'val': 0.07; 'commonly': 0.09; 'none.': 0.09; 'to:addr:comp.lang.python': 0.09; 'used).': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; 'value.': 0.15; '"none"': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'count)': 0.16; 'counter()': 0.16; 'execute,': 0.16; 'expression.': 0.16; 'folks,': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'yield': 0.17; '>>>': 0.18; 'otherwise,': 0.20; 'trying': 0.21; 'resumes': 0.22; 'sends': 0.22; 'monday,': 0.23; 'seems': 0.23; '(this': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '----': 0.27; 'cc:addr:gmail.com': 0.27; 'usually': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'received:google.com': 0.34; 'thanks': 0.34; 'feed': 0.35; 'next': 0.35; 'method': 0.36; 'should': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'skip:u 10': 0.60; 'first': 0.61; 'difficulty': 0.65; '2013': 0.84; 'oscar': 0.84; 'yielded': 0.84 X-Received: by 10.49.15.38 with SMTP id u6mr664371qec.8.1361753946488; Sun, 24 Feb 2013 16:59:06 -0800 (PST) Newsgroups: comp.lang.python Date: Sun, 24 Feb 2013 16:59:06 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=168.159.144.57; posting-account=EUV6tAoAAAA_IfJF_x-XYQxCI2bc4CDF References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 168.159.144.57 MIME-Version: 1.0 Subject: Re: yield expression From: Ziliang Chen To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Ziliang Chen , Python List 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: , Message-ID: Lines: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361753950 news.xs4all.nl 6879 [2001:888:2000:d::a6]:45821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39842 On Monday, February 25, 2013 8:51:28 AM UTC+8, Oscar Benjamin wrote: > On 25 February 2013 00:39, Ziliang Chen wrote: >=20 > > Hi folks, >=20 > > When I am trying to understand "yield" expression in Python2.6, I did t= he following coding. I have difficulty understanding why "val" will be "Non= e" ? 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. >=20 > > >=20 > > Thanks ! >=20 > > >=20 > > code snippet: >=20 > > ---- >=20 > > def counter(start_at=3D0): >=20 > > count =3D start_at >=20 > > while True: >=20 > > val =3D (yield count) >=20 > > if val is not None: >=20 > > count =3D val >=20 > > else: >=20 > > print 'val is None' >=20 > > count +=3D 1 >=20 >=20 >=20 > The value of the yield expression is usually None. yield only returns >=20 > a value if the caller of a generator function sends one with the send >=20 > method (this is not commonly used). The send method supplies a value >=20 > to return from the yield expression and then returns the value yielded >=20 > by the next yield expression. For example: >=20 >=20 >=20 > >>> g =3D counter() >=20 > >>> next(g) # Need to call next() once to suspend at the first yield cal= l >=20 > 0 >=20 > >>> g.send('value for count') # Now we can send a value for yield to ret= urn >=20 > 'value for count' >=20 >=20 >=20 >=20 >=20 > Oscar Thanks Oscar ! I am cleared. Only when "send" is used to feed "yield" a new value, the "yi= eld" expression has none "None", otherwise, "yield" expression has "None" v= alue.