Big time WTF with generators - bug?

James Stroud jstroud at mbi.ucla.edu
Wed Feb 13 03:57:15 EST 2008


Paul Rubin wrote:
> James Stroud <jstroud at mbi.ucla.edu> writes:
>> I defined a little debugging function called iterprint:
>>
>> def iterprint(thing): ...
>>        for x in thing:
>>          iterprint(x)
> 
> of course this mutates the thing that is being printed.  Try using
> itertools.tee to fork a copy of the iterator and print from that.
> I didn't look at the rest of your code enough to spot any errors
> but take note of the warnings in the groupby documentation about
> pitfalls with using the results some number of times other than
> exactly once.

I can see I didn't explain so well. This one must be a bug if my code 
looks good to you. Here is a summary:

- If I iterprint inside the loop, iterprint looks correct.
- If I iterprint outside the loop, my generator gets consumed and I am 
only left with the last item, so my iterprint prints only one item 
outside the loop.

Conclusion: something consumes my generator going from inside the loop 
to outside.

Please note that I am not talking about the yielded values, or the 
for-loop that creates them. I left them there to show my intent with the 
function. The iterprint function is there to show that the generator 
gets consumed just moving from inside the loop to outside.

I know this one is easy to dismiss to my consuming the generator with 
the iterprint, as this would be a common mistake.

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com



More information about the Python-list mailing list