-->

Generators in Python

By: Priya Philip 4 weeks, 1 day ago

Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.

Mainly two concepts:

Generator-Function : A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn.

Differences between Generator function and a Normal function

Here is how a generator function differs from a normal function.

  • It contains one or more yield statement.
  • When called, it returns an object (iterator) but does not start execution immediately.
  • Methods like __iter__() and __next__() are implemented automatically. So we can iterate through the items using next().
  • Once the function yields, the function is paused and the control is transferred to the caller.
  • Local variables and their states are remembered between successive calls.
  • Finally, when the function terminates, StopIteration is raised automatically on further calls.

Here is an example to illustrate all of the points stated above. We have a generator function named my_gen() with several yield statements.

 
# A generator function that yields 1 for first time, 
 # 2 second time and 3 third time 
 def my_gen(): 
     yield 1            
     yield 2            
     yield 3            
    
 # Driver code to check above generator function 
 for val in my_gen():  
     print(val) 

Output:

1
2
3

Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop.

Why generators are used in Python?
1. Easy to Implement
Generators can be implemented in a clear and concise way as compared to their iterator class counterpart.

Example: Implement a sequence of power of 2's using Generator function.


def PowerGen(max = 0):
    p = 0
    while p < max:
        yield 2 ** p
        p += 1
2. Memory Efficient
Generator implementation  is memory friendly and is preferred since it only produces one item at a time.
3. Represent Infinite Stream
Generators are excellent medium to represent an infinite stream of data. Infinite streams cannot be stored in memory and since generators produce only one item at a time, it can represent infinite stream of data.

Example: Generate all the even numbers


def all_even():
    n = 0
    while True:
        yield n
        n += 2
4. Pipelining Generators
Generators can be used to pipeline a series of operations.

Comments

Wish to hire us?

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.