Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65396 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2014-02-03 19:55 -0500 |
| Last post | 2014-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.
Re: generator slides review Terry Reedy <tjreedy@udel.edu> - 2014-02-03 19:55 -0500
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-02-03 19:55 -0500 |
| Subject | Re: 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
Back to top | Article view | comp.lang.python
csiph-web