copying generatrors

Horace Enea hjenea at earthlink.net
Tue Jun 5 20:31:40 EDT 2007


My example wasn't very good. Here's another try:

def foo():
   yield 1
   yield 2
   yield 3

f = foo()
f.next()
1

g=copy(f)  # copy the generator after an iteration

f.next()
2
f.next()
3

g.next()      
2

I want to copy the generator's state after one or more iterations.





In article <1181087423.361999.324710 at o11g2000prd.googlegroups.com>,
 Matimus <mccredie at gmail.com> wrote:

> Why not just do this:
> 
> >>> def foo():
> ...  yield 1
> ...  yield 2
> ...  yield 3
> ...
> >>> f = foo()
> >>> g = foo()
> >>> f.next()
> 1
> >>> f.next()
> 2
> >>> f.next()
> 3
> >>> g.next()
> 1
> >>> g.next()
> 2
> >>> g.next()
> 3



More information about the Python-list mailing list