PEP 255: Simple Generators
Just van Rossum
just at letterror.com
Thu Jun 21 14:27:41 EDT 2001
Terry Reedy wrote:
> When I first read the PEP, I assumed that def'ed generators would somehow
> return the successive yielded values on successive calls (wrong). [ ... ]
and further:
> Question: am I correct in thinking that separating iterator-generation from
> iterator-first-use makes it possible to have multiple iterators from the
> same generator in simultaneous use, as in
>
> sq10 = squares(10)
> sq20 = squares(20)
> ...
> ?
That, but it's also to ensure thread-safety and allow recursion, just like
with "normal" functions. When you call a normal function, a stack frame is
created which is immediately executed. When you call a generator function,
a stack frame is created, which is returned immediately, wrapped in an
iterator object.
Just
More information about the Python-list
mailing list