Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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; 'none:': 0.05; 'assign': 0.07; 'caller': 0.07; 'val': 0.07; 'commonly': 0.09; 'none.': 0.09; 'used).': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; '"none"': 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; 'trying': 0.21; 'resumes': 0.22; 'sends': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; '(this': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '----': 0.27; 'message-id:@mail.gmail.com': 0.27; 'usually': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'method': 0.36; 'should': 0.36; 'why': 0.37; 'received:209': 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=i98D4FLFXrqqPLcV9Q4FQYxoHBbYEH+DG4lY8b95Jtc=; b=fnb7EHXww9pAk/NHht4gTyfqrlXvQ5eqD8QUdxMpJET8iMAF86OEcnjAc1zmNgFsx+ 9dO+aVxo89rmbzaiFaamw3KRkdKzTAfQBQzKAwKpWOEukgvwR6//fGm0xABdQ/ejLPu6 m5OAtSFs7KsvC0WkoO9qP4eA4LqQGGo5KGaWbm3yrCgwXBLc3BkQQ6nkVTFKkFezdYm9 1lptdGUoMd9c1OsUG+s4od5xzz1MAZXjtM4e/NpVlyjsxrTXQgRK2UOxb19L9heEgekg M0Im/vokKbFpwmUjibyxK5wRcm3Yk7C3HMo1nEBHUcOsDo+KA6bIx/iZyBtluzZHPOQg jdDg== X-Received: by 10.58.155.99 with SMTP id vv3mr9177763veb.50.1361753508480; Sun, 24 Feb 2013 16:51:48 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Mon, 25 Feb 2013 00:51:28 +0000 Subject: Re: yield expression To: Ziliang Chen Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: 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: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361753515 news.xs4all.nl 6957 [2001:888:2000:d::a6]:37833 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39840 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 re= sumes to execute, it will assign "count" to "val", so "val" should NOT be "= None" all the time. > > Thanks ! > > code snippet: > ---- > def counter(start_at=3D0): > count =3D start_at > while True: > val =3D (yield count) > if val is not None: > count =3D val > else: > print 'val is None' > count +=3D 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 =3D 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 retur= n 'value for count' Oscar