A genertor function, which acts as an iterator, provides memory-efficient way to generate a large sequence of data, such as an array with size of 100,000. We have 3 different methods to invoke generators in Python.

  • use yield

In [8]:
def firstn(n):
    num = 0
    while num < n:
        yield num
        num += 1
        
print(sum(firstn(1000000)))


499999500000
  • use build-in generator functions, such as xrange()
  • we can also do generator comprehension.

In [10]:
y = (x*2 for x in [1,2,3,4,5])
print y.next()


2

To implement a iterator, we have to define a class with two specific functions: 1) __iter__ method that returns a iterator (self); 2) next method to generate next instance when the iterator is called in the for loop.


In [3]:
import random

class randomwalker_iter:
    def __init__(self):
        self.last = 1
        self.rand = random.random()
        
    def __iter__(self):
        return self
    
    def next(self):
        if self.rand < 0.1:
            raise StopIteration
        else:
            while abs(self.rand - self.last) < 0.4:
                self.rand = random.random()
            self.last = self.rand
            return self.rand
        
rw = randomwalker_iter()
for rw_instance in rw:
    print rw_instance


0.383538446634
0.890320655611
0.440146898917
0.870770177206
0.254437490207
0.815695129332
0.193317108258
0.941581533152
0.527148315011
0.95593994999
0.276660639832
0.738954693986
0.0527577562047