Jessica
Jessica

Reputation: 2415

Generators and for loops in Python

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

Upvotes: 22

Views: 5356

Answers (3)

vaultah
vaultah

Reputation: 46523

generator() initializes new generator object:

In [4]: generator() is generator() # Creating 2 separate objects
Out[4]: False

Then generator().next() gets the first value from the newly created generator object (0 in your case).

You should call generator once:

In [5]: gen = generator() # Storing new generator object, will reuse it

In [6]: [gen.next() for _ in range(6)] # Get first 6 values for demonstration purposes
Out[6]: [0, 0, 0, 0, 0, 1]

Note: generator.next was removed from Python 3 (PEP 3114) - use the next function instead:

In [7]: next(gen)
Out[7]: 1

Upvotes: 48

PascalVKooten
PascalVKooten

Reputation: 21433

I posted it in the comment, I think if you look at this in parallel with the question you see the mistake:

with open('my_file.txt', 'r') as f: 
    print(f.readline())               # `readline` simply reads a single line

with open('my_file.txt', 'r') as f: 
    print(f.readline())

with open('my_file.txt', 'r') as f: 
    print(f.readline())

with open('my_file.txt', 'r') as f: 
    print(f.readline())    

Instead of making a new generator object each time, you have to make it once and then use it until it has been depleted:

mygen = generator()
mygen.next()
mygen.next()
...
etc

Upvotes: 2

Malik Brahimi
Malik Brahimi

Reputation: 16711

With each call of generator you are creating a new generator object:

generator().next() # 1st item in 1st generator 
generator().next() # 1st item in 2nd generator

Create one generator, and then call the next for subsequent items:

g = generator()

g.next() # 1st item in 1st generator
g.next() # 2nd item in 1st generator

Upvotes: 17

Related Questions