So I have a generator function, that looks like this.
def generator():
while True:
for x in range(3):
for j in range(5):
yield x
After I load up this function and call "next" a bunch of times, I'd expect it to yield values
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 0 0 0 0 0 ...
But instead it just yields 0 all the time. Why is that?
>>> execfile("test.py")
>>> generator
<function generator at 0x10b6121b8>
>>> generator().next()
0
>>> generator().next()
0
>>> generator().next()
0
>>> generator().next()
0
>>> generator().next()
0
>>> generator().next()
0
>>> generator().next()
0
next()
andprint
would certainly not be so spectacular to need 14 upvotes and 2 favorites...generator()
call returns a new generator object which starts at its beginning. Trygen = generator()
, thengen.next()
,gen.next()
, etc.