Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'insert': 0.05; 'debug': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; 'iterable': 0.16; 'once.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'steps,': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'example': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; '(which': 0.31; 'code': 0.31; 'away.': 0.31; 'another': 0.32; 'ago': 0.33; 'maybe': 0.34; 'could': 0.34; 'common': 0.35; 'connection': 0.35; 'but': 0.35; 'yield': 0.36; 'done': 0.36; 'possible': 0.36; 'similar': 0.36; 'error.': 0.37; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'initially': 0.38; 'same.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'days': 0.60; 'deleting': 0.60; 'received:173': 0.61; 'between': 0.67; '10:': 0.84; 'andrea': 0.84; 'received:fios.verizon.net': 0.84; 'anymore,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: generator slides review Date: Mon, 03 Feb 2014 19:55:03 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391475324 news.xs4all.nl 2919 [2001:888:2000:d::a6]:50786 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65396 On 2/3/2014 5:22 PM, andrea crotti wrote: > That's already better, another thing which I just thought about could > be this (which actually happened a few times): > > def original_gen(): > count = 0 > while count < 10: > yield count > count += 1 > > > def consumer(): > gen = original_gen() > # lis = list(gen) > for n in gen: > print(n * 2) > > if I uncomment the line with "lis = list(gen)" > it won't print anything anymore, because we have to make sure we only > loop over ONCE. > That maybe is a better example of possible drawback? (well maybe not a > drawback but a potential common mistake) A couple of days ago I made a similar error. Stdlib code a = list(f(iterable1)) return g(a) # g just need an iterable The return value is buggy. Insert for i in a: print(i) to debug. Notice that g does not need a concrete list. Delete list wrapper. Return value goes away. Because I did other things between the last two steps, I did not immediately make the connection and was initially puzzled. Deleting debug code made things work again. Using itertools.tee would have done the same. -- Terry Jan Reedy