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; 'method.': 0.05; 'none:': 0.05; 'assign': 0.07; 'val': 0.07; 'python': 0.09; '"("': 0.09; 'def': 0.10; 'value.': 0.15; '")"': 0.16; '"=")+': 0.16; '"none"': 0.16; '(target_list': 0.16; '::=': 0.16; 'count)': 0.16; 'execute,': 0.16; 'folks,': 0.16; 'statement.': 0.16; 'subject:expression': 0.16; 'subject:yield': 0.16; 'true:': 0.16; 'yield_stmt': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'trying': 0.21; 'resumes': 0.22; 'seems': 0.23; 'allows': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; '----': 0.27; 'am,': 0.27; 'statements': 0.29; 'code': 0.31; 'url:python': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'produced': 0.33; 'thanks': 0.34; 'pm,': 0.35; 'url:org': 0.36; 'should': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'difficulty': 0.65; 'believe': 0.69; 'received:74.208': 0.71; "it'd": 0.84; 'url:reference': 0.84 Date: Tue, 26 Feb 2013 12:05:15 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: yield expression References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:Zjr+PQOa6mSbKBDhFKp2A+dnEnjKic5pg0teA1cTQvP 4dxUl7cZnZ2GOIynf7JS58kXFEpUrAU4QvFsPCeKkImOefVZ4K LzDmL4xUoS0ked2conKeyxMWi0v5Ezoz0PjKmqXBVtOyc0CrOq BS41Rm7tRraHg3I3klJELMLe47K1Tzpj/pMrIp7YcGfOYKZXBq KFHngITsQgod5S+SJv8ImWNiCW1jcHzLq607/ZjejDMc64/7w8 RSMonnt5q6ZkcH5CEE30FKDbwnMWOBNF4YRHNnhIo/iZxyYX5l TLlQafI77yaJ0lACeSd3kWEqu9dQVOoDXKrydLOjggpFFoOKA= = 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361898338 news.xs4all.nl 6879 [2001:888:2000:d::a6]:34866 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39973 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