I have always heard about python generators , now i got a chance to study about this generators . I think generators are one of important and advanced feature in python .What are these generators ? A generator is a function that produce a sequence of result instead of a single value while maintainig the state of function. Generators are very useful and simplfy our code also improve the performance. Most typical use of generator is to define iterator ,iterators are fundamental part of python and they are used in the ‘for’ statement. To defining a generator we must know about iterator and its working because generators are powerful tool to create iterators. In this article i explaining about concepts of generators how it improve efficiency of python.
What is iterators ?
We are familiar with iterations, because they are fundamental concept in python .When we create a list we can read its items one by one .This process is called iteration. Python allow many type of iterations such as iterating over list ,dictionary ,file etc. But we don’t know what happen behind a for loop. The for statement call iter() function and this function return an object that have a .next() method which can access element one at a time and it raise StopIteration exception when there is no element and tell to for loop to terminate. This is the basic working of iterator. When we read values using iteration all values are stored in memory and we can read as our wish .
What is a generator ?
Generators are iterable but we can only read values once. Its because they do not store all the values in memory. We can also define generator as a function that remember the point in the function body where it last returned.
Eg :
def countdown(n):
while n > 0:
yield n
n -= 1
Calling a generator function create and return a generator object.
>>>x=countdown(3) >>>print x <generator object at 0x58490>
It does not start running the function ,instead it create a generator object .Also the function not really exit and it goes to a suspended state . Then for loop try to loop over this generated object , then function resume from its suspended state and run until the next yield statement and return that as the next item. This procedure will happen until the function exits and at which point the generator raises StopIteration , and the loop exits. In the above function named countdown(3) which print first 3 and then generator forgot 3 and then print 2 , that also forgot and last print 1. So that improved the performance of code because less memory is used . We can only once read values using generator object. If we want to read again all these values then we need to create again a generator object .
Let us look about importance of yield statement in a generator function
Yield statement
Yield statement is an important part of a generating function which have following specifications,
- Only used in body of generating functions
- Using a yield statement in a function definition causes that definition to create a generating function instead of normal function
- Yield statement resume where the function left of
- When a yield statement is executed state of a generator function is frozen and next time .next() function invoked the function will resume
Now we can try to run a for loop over the generator object of above generating function.
>>> x.next() 3 >>>x.next() 2 >>>x.next() 1 >>>x.next() Traceback (most recent call last): File "<stdin>", line 1, in ? StopIteration
Next we want to look about generated expressions
Generated expressions
Generated expressons same as list comprhensions in python . But some differences are there which are follows,
- Generator function do not construct a list
- Only useful purpose is iteration
- Once consumed ,then can’nt reused
- Instead of bracket parentheses is used
>>>a=[1,2,3,4] >>>>c=(2*x for x in a) <generator object at 0x58760> >>>for i in a: print i 2 4 6 8 >>>sum(i*i for i in range(10)) 285
So generator function make python more effective and efficient and i hope that this article will help you to learn about some informations about generators in python ,
Pingback: Python list comprehension | neethutv