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


Groups > comp.lang.python > #65396 > unrolled thread

Re: generator slides review

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-02-03 19:55 -0500
Last post2014-02-03 19:55 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#65396 — Re: generator slides review

FromTerry Reedy <tjreedy@udel.edu>
Date2014-02-03 19:55 -0500
SubjectRe: generator slides review
Message-ID<mailman.6374.1391475324.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web