I would like to know how to reinitialize/reset a generator. I mean, if I for example have:
And then i run
If it is possible, i would thank you a lot if you can link the documentation file with the info, because i have searching for it so long and I didn't find anything.
P.D. Maybe my english is not good enought and i misspelled some words. Sorry about that.
Generators come from python, and I found a similar question at stackoverflow:
The basic answer seems to be that generators can't be reset. Instead, you could re-run the code which creates the generator to make a fresh copy of it, or you could store the outputs of the generator in a list so that you can reuse them. If neither of these will work, you could perhaps modify how the generator is built so that re-creating it does not take too long (for example, do computationally intense parts first, store the output, and use that to create the generator).
UPDATE: For the particular example you gave, it's easy enough to just recreate the generator:
Feel free to post more details about your particular problem if you want :)
good luck! Niles
First one point: in general you probably don't want to use "range" in a generator expression because that actually builds the list, and avoiding list construction is one of the reasons people use generators and iterators in the first place. You often have to have a pretty sizable array to notice the difference, I admit:
Note that there are int construction overheads the first time, and for small range size (for some definition of small) the difference won't matter -- range might even be faster -- but it's still going through the loop, so it'll break for really big sizes even if you only ever look at a few values. Try w=10**9 and prepare to wait..
As for your real question, I don't know of a good way to clone a generator expression using the standard tools. (A ".restart()" method wouldn't make any sense in many cases, which is one of the reasons it's not part of the protocol.) You can use itertools.tee to make multiple cached copies of the output, but since it's caching the values to do the trick you could have memory problems or accuracy problems if the function is nondeterministic.
But if you want to reinitialize the generator and start over, why not simply redeclare it instead? If you dislike the code duplication, you could wrap it in a function:
which I think should have mostly the same effect.
[UPDATE: Oops, didn't update my window, so didn't see Niles already said the same thing!]
Thanks both of you for the answers.
I have been told there must be a way to reset a generator without re-declaring it, and i have to find it :S I have been for long time on the doc searching, but I either found the way to reset it, or that there is no way to do that.
Could you link me from the documentation where it is said that you cannot reset a generator? (or how to, if it is actually possible)
posted Jan 02 '11Tlk
21 ● 1 ● 1 ● 4
Asked: Jan 01 '11
Seen: 546 times
Last updated: Jan 03 '11
powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.