#!/usr/bin/env python # coding: utf-8 # 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 # In[4]: h = foo(5) # The next() builtin function for iterators is available in both Python 2 and 3. # In[5]: next(g) # The .next() method for iterators is available only in Python 2. # In[6]: g.next() # In[7]: next(g) # 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) # In[10]: next(g) # In[11]: next(g)