Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65396

Re: generator slides review

From Terry Reedy <tjreedy@udel.edu>
Subject Re: generator slides review
Date 2014-02-03 19:55 -0500
References <CAF_E5JYWZXkq98ri63BMQst3kpPVhO1K5xQbQ0ZHCuRTYZXvUA@mail.gmail.com> <lck5pr$reg$1@ger.gmane.org> <CAF_E5JbHE_RC79M09WDWhB5PYJeo_qs-Z4yWZQCqvRQJLpdm3Q@mail.gmail.com> <lcnfqq$2cr$1@ger.gmane.org> <CAF_E5JZuTeP-+zjbcjC49NEOtrMzi7kDy66QrHmP8oWDjsoovA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6374.1391475324.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: generator slides review Terry Reedy <tjreedy@udel.edu> - 2014-02-03 19:55 -0500

csiph-web