In [1]:
def foo(n):
    for i in range(n):
        yield i*i

In [2]:
g = foo(4)
g


Out[2]:
<generator object foo at 0xb34e19dc>

In [3]:
h = foo(5)

The .next() method for iterators is available only in Python 2.


In [4]:
g.next()


Out[4]:
0

The next() builtin function for iterators is available in both Python 2 and 3.


In [5]:
next(g)


Out[5]:
1

The h and g generators are independent of each other. So iterating through h does not iterfere with g.


In [6]:
for i in h:
    print i


0
1
4
9
16

In [7]:
g.next()


Out[7]:
4

In [8]:
g.next()


Out[8]:
9

In [9]:
g.next()


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-9-d7e53364a9a7> in <module>()
----> 1 g.next()

StopIteration: