.feed-links {display:none !important;} -->

Python iterators and generators

 

What is an iterator?

An iterator is a kind of cursor whose mission is to move through a sequence of objects. The iterator allows you to walk through each object in a sequence without worrying about the underlying structure.

Why use an iterator rather than a list?

The iterator brings a higher level of abstraction, ie we add an extra layer of code to perform an action. If one day we had to change elements of the code - example of an update of a lib or change of database for example - we do not need to change all our code since this layer of abstraction in addition allows you to change only what is necessary at the level of the class and no longer of the code. Best of all, the iterator is inexpensive in terms of memory usage.

Create your own iterator

A list is an iterator:

>>>  list  =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] 
>>>  for  x  in  list : 
...      print ( x ) 
...  
1 
2 
3 
4 
5 
6 
7 
8 
9 
10

Let's create our own iterator class:

# coding: utf-8

class  MonIter ():
    
    current = 0

    def  __init__ ( self , stop ): 
        self . stop = stop

    def  __iter__ ( self )  : 
        return  self

    def  next ( self )  : 
        self . current + =  1
        
        if  self . current > self . stop : 
            raise  StopIteration

        if  self . current  ==  5 : 
            print ( "What already 5th round?" )

        return  self . current

We can now loop over this element:

>>>  for  i  in  moniter ( 10 ): 
...      print ( i ) 
...  
1 
2 
3 
4 
What  already  5 th  round ? 
5 
6 
7 
8 
9 
10

Generators

Generators make it easier to create iterators.

Let's create a basic generator:

# coding: utf-8

def  generator (): 
    yield  "a" 
    yield  "b" 
    yield  "c"

Then let's create an iterator:

>>>  i = generator () 
>>>  for  v  in  i : 
...      print ( v ) 
...  
a 
b 
c

So what the hell is this we don't understand. First we remaque there is a new keyword, created especially for the generators: yield This keyword is a bit similar to return functions except that it does not mean the end of the execution of the function but a pause and at the next iteration the function will search for the next one yield .

Let's create the same iterator as in the previous chapter:

# coding: utf-8

def  generator ( n ): 
    for  i  in  range ( n ): 
        if  i  ==  5 : 
            print  "What already 5th round?" 
        yield  i + 1

Now let's loop through our iterable:

>>>  i = generator ( 10 ) 
>>>  for  v  in  i : 
...      print ( v ) 
...  
1 
2 
3 
4 
5 
What  already  5 th  round ? 
6 
7 
8 
9 
10

Voila, we arrive at the same result but with much less lines of code.

No comments:

Post a Comment