revive a generator
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Oct 21 09:48:43 EDT 2011
On Thu, 20 Oct 2011 19:09:42 -0700, Yingjie Lan wrote:
>> Here's an example of an explicit request to revive the generator:
>
>
>>>>> g = (x*x for x in range(3))
>>>>> for x in g: print x
>> 0
>> 1
>> 4
>>>>> g = (x*x for x in range(3)) # revive the generator for x in g:
>>>>> print x #now this will work
>> 0
>> 1
>> 4
>>
>> ChrisA
>
>
> What if the generator is passed in as an argument when you are writing a
> function? That is, the expression is not available?
If the expression is not available, how do you expect to revive it? The
expression is gone, it no longer exists. As you said in another post:
"What if the generator involves a variable from another scope,
and before re-generating, the variable changed its value."
Exactly. In general, you *can't* revive general iterators. It simply
isn't possible. The variables that defined it might be gone. They might
be non-deterministic: random numbers, data from the Internet or a file
system that has changed, or user input. Trying to enforce the rule
"iterators must support restarting" is foolish: it can't be done. You use
an iterator when you want to iterate over something *once*, that is why
they exist. If you want to iterate over it twice, don't use an iterator,
use a sequence.
Forcing all iterators to save their data, on the off-chance that maybe
somebody might want to iterate over it twice, defeats the purpose of an
iterator.
> Secondly, it would be nice to automatically revive it. For example, when
> another for-statement or other equivalent is applied to it.
Nice? No, it would be horrible. It goes against the basic concept of an
iterator: iterators should be memory efficient, generating values lazily.
If you want an iterable sequence that you can iterate over multiple
times, then use a list, or a custom iterable class.
If you want a socket wrench, use a socket wrench. Don't insist that
hammers have to have a socket wrench attachment.
--
Steven
More information about the Python-list
mailing list