468,294 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

copying generatrors

Does anyone have code to copy a generator?

Here is what I'd like to do:

def foo():
yield 1
yield 2
yield 3

f = foo()
g = copy(foo)

print f.next()
1
print f.next()
2

print g.next()
1

Thanks,

Horace
Jun 5 '07 #1
4 947
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

Jun 5 '07 #2
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 <11**********************@o11g2000prd.googlegroups .com>,
Matimus <mc******@gmail.comwrote:
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
Jun 6 '07 #3
Horace Enea wrote:
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.
You could use itertools.tee():
>>def foo():
.... yield 1
.... yield 2
.... yield 3
....
>>import itertools
f = foo()
f.next()
1
>>f, g = itertools.tee(f)
f.next()
2
>>f.next()
3
>>g.next()
2
>>g.next()
3

But note that if your iterators get really out of sync, you could have a
lot of elements stored in memory.

STeVe
Jun 6 '07 #4
Steve,

Hey, thanks. I'll try that.

Horace

In article <-J******************************@comcast.com>,
Steven Bethard <st************@gmail.comwrote:
Horace Enea wrote:
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.

You could use itertools.tee():
>>def foo():
... yield 1
... yield 2
... yield 3
...
>>import itertools
>>f = foo()
>>f.next()
1
>>f, g = itertools.tee(f)
>>f.next()
2
>>f.next()
3
>>g.next()
2
>>g.next()
3

But note that if your iterators get really out of sync, you could have a
lot of elements stored in memory.

STeVe
Jun 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Thomas Lotze | last post: by
3 posts views Thread by Robert Tarantino | last post: by
13 posts views Thread by franky.backeljauw | last post: by
22 posts views Thread by Matt | last post: by
reply views Thread by surya | last post: by
1 post views Thread by Mark Smith | last post: by
1 post views Thread by emilysmith10 | last post: by
1 post views Thread by RonLandreth | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.