In [1]:
def foo(n):
for i in range(n):
yield i*i
In [2]:
g = foo(4)
g
Out[2]:
In [3]:
h = foo(5)
The .next() method for iterators is available only in Python 2.
In [4]:
g.next()
Out[4]:
The next() builtin function for iterators is available in both Python 2 and 3.
In [5]:
next(g)
Out[5]:
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
In [7]:
g.next()
Out[7]:
In [8]:
g.next()
Out[8]:
In [9]:
g.next()