In [1]:
from __future__ import print_function
In [2]:
def foo(n):
for i in range(n):
yield i*i
In [3]:
g = foo(4)
g
Out[3]:
In [4]:
h = foo(5)
The next() builtin function for iterators is available in both Python 2 and 3.
In [5]:
next(g)
Out[5]:
The .next() method for iterators is available only in Python 2.
In [6]:
g.next()
In [7]:
next(g)
Out[7]:
The h and g generators are independent of each other. So iterating through h does not iterfere with g.
In [8]:
for i in h:
print(i)
In [9]:
next(g)
Out[9]:
In [10]:
next(g)
Out[10]:
In [11]:
next(g)