how to make a generator use the last yielded value when it regains control

John Salerno johnjsal at NOSPAMgmail.com
Thu Apr 6 22:31:02 EDT 2006


John Salerno wrote:

> 1. repeat indefinitely and have the number of times controlled elsewhere 
> in the program (e.g., use the morris() generator in a for loop and use 
> that context to tell it when to stop)
> 
> 2. just make it a function that takes a second argument, that being the 
> number of times you want it to repeat itself and create numbers in the 
> sequence

Well, I suppose I could just do:

num = 1
for x in range(some_limit):
     num = morris(num)
     print num,

But that isn't as much of a challenge as the other two options. :) I'd 
like the function to do all the work of returning multiple numbers 
(which probably means that option 1 isn't the best either)



More information about the Python-list mailing list