Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Saturday 13 September 2014

Undestanding Generators in Python

September 13, 2014 Posted by Dinesh ,
Before we understand generator we must know Iterator.

Iterator:
Iterator is any object which understands the concept of a for-loop.
Basically this means that it has a next method, when it is called it returns the next item in the sequence.
Everything you can use in "for" is an iterable: lists, strings, files...etc

>>> [x*x for x in range(3)]
[0, 1, 4]


>>> mylist = [x*x for x in range(3)]
>>> for i in mylist:
    print i


0
1
4


Generator:
Generators are iterators, but you can only iterate over them once.
It's because they do not store all the values in memory, they generate the values on the fly.

If we try to print the generator we will get the object.

>>> (x*x for x in range(3))
<generator object <genexpr> at 0x000000000268C480>


>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator:
 print i
0
1
4


Generators can be iterated only once. If we try to iterate over them again we get nothing its because when for loop is running it actually calls next element in the tuple and loop will break when there are no more elements. Once the loop breaks generator is destroyed (object still exist but no more elements to return). So if we try to use the same generator again to print the elements we get nothing.

>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator:
 print i
0
1
4

>>> for i in mygenerator:
 print i


In short,  if round parentheses are used, then a generator iterator is created.
If rectangular parentheses are used, then we get a list.