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; 'none:': 0.05; 'received:134': 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; 'question.': 0.20; '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; 'header :User-Agent:1': 0.26; 'values': 0.26; '----': 0.27; 'finds': 0.29; 'helpful.': 0.29; 'sleep': 0.29; 'statements': 0.29; 'url:mailman': 0.29; 'starts': 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; 'thanks': 0.34; 'saved': 0.35; 'pm,': 0.35; 'next': 0.35; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'gives': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'url:mail': 0.40; 'skip:u 10': 0.60; 'back': 0.62; 'more': 0.63; 'making': 0.64; '26,': 0.65; 'difficulty': 0.65; '2013': 0.84 Date: Tue, 26 Feb 2013 13:44:06 -0500 From: "Colin J. Williams" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 Newsgroups: comp.lang.python To: "Vytas D." Subject: Re: yield expression References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-NCF-Filtered: By ProxSMTP on pallando Tue Feb 26 13:44:10 2013 -0500 (EST) 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: , Message-ID: Lines: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361904259 news.xs4all.nl 6874 [2001:888:2000:d::a6]:55036 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39988 On 26/02/2013 12:07 PM, Vytas D. wrote: > 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 > > > Yes, it's very helpful. Thanks also to the other two responders. This brings us back to the OP question. Why not " val = (yield count)"? Colin W.