Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'value,': 0.03; 'none:': 0.05; '__name__': 0.07; 'assign': 0.07; 'main()': 0.07; 'val': 0.07; 'none.': 0.09; 'def': 0.10; 'value.': 0.15; '"""returns': 0.16; '"none"': 0.16; "'__main__':": 0.16; 'count)': 0.16; 'counter()': 0.16; 'earlier.': 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; 'statement': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; '----': 0.27; "doesn't": 0.28; '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; 'true.': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'another': 0.33; 'wrong': 0.34; 'thanks': 0.34; 'saved': 0.35; 'pm,': 0.35; 'next': 0.35; 'explain': 0.36; 'but': 0.36; 'url:org': 0.36; "wasn't": 0.36; "didn't": 0.36; 'method': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'possible': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'nothing': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'url:mail': 0.40; 'skip:u 10': 0.60; 'most': 0.61; 'back': 0.62; 'more': 0.63; 'making': 0.64; '26,': 0.65; 'difficulty': 0.65; 'received:74.208': 0.71; '2013': 0.84; 'observed': 0.84; 'received:74.208.4.194': 0.84 Date: Tue, 26 Feb 2013 21:59:31 -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: <512D0276.5040909@ncf.ca> In-Reply-To: <512D0276.5040909@ncf.ca> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:QmgTE1VFMgav1+ZIDB/bUcZgieXP+NrKt7gBATrbT28 ljIWkIFR1amwapaaiQ+aKOCNOJBeopZVq1tF1eQow4lvwdzmui XZINvxq9/tChSfCGJbaO61zBK4JrRcwOu3N9/jNuOR2z7rzXL3 fpFixQzWdQu9Hfm+GCkO01/rIIPk74jwX+dfJrgvnoZB68eADX ibAskNEI6SjYZSwGwizNKX1oGNoa7Sg7YkzM3LA1MkaxHU5B0H sCK5yf5DSHDyE+Ey6RdtEbm0UcJEULyKfEEYKToN1oznVEJQMR TZHelPZuHACrUapv7cT+QTnaPJ6ozvwEhQWOu6OapwiOmtGdw= = 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: 105 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361933994 news.xs4all.nl 6905 [2001:888:2000:d::a6]:36208 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40025 On 02/26/2013 01:44 PM, Colin J. Williams wrote: > 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. > > Repeating a misconception doesn't make it any more true. yield is both an expression and a statement. Nothing wrong with the statement val = (yield count) although he apparently wasn't making any good use of the possible return value, since he observed it was always None. That's just because he didn't use the send method in the calling function. Most links I see on a web search explain only the expression form of yield, which is why i took a lot of time building the response I did earlier. -- DaveA