breaking iteration of generator method

B

Brian

Hello;

What happens when a program breaks (think keyword 'break') its iteration
of a generator? Is the old state in the generator preserved?

Ex:

# .gen() is generator method.

for i in myObject.gen():
if i == specialValue:
break

Do subsequent calls to .gen() start off with new locals? Or, would
the iteration begin after the yielded 'i' that caused the for-statement
to exit?


Many thanks,

Brian.
 
H

Hans Nowak

Brian said:
Hello;

What happens when a program breaks (think keyword 'break') its iteration
of a generator? Is the old state in the generator preserved?

Ex:

# .gen() is generator method.

for i in myObject.gen():
if i == specialValue:
break

Do subsequent calls to .gen() start off with new locals? Or, would
the iteration begin after the yielded 'i' that caused the for-statement
to exit?

It's easy to find out... :)
.... for i in range(10):
.... yield i
....
.... if x == 4:
.... break
.... else:
.... print x,
....
0 1 2 3.... print x,
....
5 6 7 8 9
However, if you use g() rather than an existing generator object, a new
generator will be created:
.... if x == 4:
.... break
.... else:
.... print x,
....
0 1 2 3.... print x,
....
0 1 2 3 4 5 6 7 8 9
HTH,
 
T

Tim Hochberg

Brian said:
Hello;

What happens when a program breaks (think keyword 'break') its iteration
of a generator? Is the old state in the generator preserved?

Ex:

# .gen() is generator method.

for i in myObject.gen():
if i == specialValue:
break

Do subsequent calls to .gen() start off with new locals?

It depends on how you do it. This::


for i in myObject.gen():
if i == specialValue:
break
# ...
for i in myObject.gen():
if i == otherSpecialValue:
break
# ...

Will start start with a new generator with new local each time since
each call to gen() creates a fresh generator. If you want to preserve
generator state between loop, what you do is::

generator = myObject.gen()
for i in generator:
if i == specialValue:
break
# ...
for i in generator:
if i == otherSpecialValue:
break
# ...

In this case, the same generator is used in each loop and the state is
preserved.

-tim
 
P

Peter Otten

Brian said:
Hello;

What happens when a program breaks (think keyword 'break') its iteration
of a generator? Is the old state in the generator preserved?

Ex:

# .gen() is generator method.

for i in myObject.gen():
if i == specialValue:
break

Do subsequent calls to .gen() start off with new locals? Or, would
the iteration begin after the yielded 'i' that caused the for-statement
to exit?


Many thanks,

Brian.

Running the following little demo should answer you questions.
The Mark class is used only to show when the generator dies, it relies on
CPython's garbage collection algorithm.

class Mark(object):
def __init__(self):
print "created"
def __del__(self):
print "gone"

def gen():
mark = Mark()
for i in range(10):
yield i


for i in gen():
print i,
if i == 5:
print "breaking"
break

g = gen()
for i in g:
print i,
if i == 5:
print "breaking"
break

while True:
print g.next()

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top