Skip to main content

Generator functions. Why and when should we use them.

You probably have heard of them. But as a beginner python programmer, we tend to ignore it's values because we fail to find uses of it. When we have loops and itertools and other fancy toys offered, why should we bother to understand them. If you want to take your programming  or understanding of Python to the next level, you must understand and use generators more and more.

Before we start, I want to make a simple program that prints square of (hence generates and delivers) 5 numbers from 1 to 5. So what is the first idea that pops up ? Let's use a for loop.


def square(N):
    for i in range(1, N+1):
        print i**2

>>>square(5)
1
4
9
16
25

Let's say, you need all the values generated for some other calculation. I know, above example is too lame . But imagine after some complicated calculations, you have generated some result.
So you probably will return all values together in an array. And to that, the first thought in our minds is, using a LIST. Let's use it. 

Now our function will look like this.

>>> def my_function(N):

    new_list = []

    for i in range(1, N+1):

        new_list.append(i**2)

    return new_list




>>> my_function(5)
[1, 4, 9, 16, 25]


All this looks easy. You probably are thinking...If generators are going to ruin this idea, I don't need it. Think again.
  1.  This LIST business will soon start becoming a nuisance as the  number of such calculations and variables increase (believe  me...thinking of new names for variables... SUCK).
  2.  What if you don't know your requirement. How many results  do you want.?
  3.  You had to create an array and it will take memory space  which becomes bigger with length of the list and size of each  element.
  4. Once you are dealing with very large list (imagine a million), generating a list of 1 million (let's assume integers) will take around 300 mb in memory.
A generator will make it all...Simpler. Here's the same example with generator function.


def square(N):
    for i in range(1, N+1):
        yield i**2

gen = square(5)

print gen.next()
print gen.next()
print gen.next()
print gen.next()
print gen.next()
print gen.next()

Result:

1
4
9
16
25

Traceback (most recent call last):

  File "D:\mydrive\Desktop\PyScripts\generators.py", line 14, in <module>
    print gen.next()
StopIteration
>>>

You can guess the reasons for the last traceback. We got out of fuel. In order to avoid this, you can always use generator object to iterate.


def square(N):
    for i in range(1, N+1):

        yield i**2

gen_object = square(5)

for result in gen_object:

    print result

Result:

1
4
9
16
25


More fun with generators:

You can also make generator functions from list comprehensions. Use parenthesis instead of square bracket to make a gen function.


l = (x**2 for x in range(1,6))  

print l
for value in l:
    print value


Result:

<generator object <genexpr> at 0x0000000002D50F78>
1
4
9
16
25

Popular posts from this blog

Why should you visit Kashmir sooner?

"If there is paradise on Earth, this is it"
And paradise it was . Early winter is the perfect time to plan for a visit to Kashmir. We will suggest to keep your itinerary  short . And enjoy the best trip of your life. 

   My Visit Month : Mid November 2015

As we were flying close to Srinagar, about 20 minutes before landing , all of a sudden , every passenger in the aircraft was on his toes craning theirs necks over the tiny window to catch a glimpse of the above. A fleet of Snow Giants.


   Visit Meena Bazaar in Dal Lake
  You will find a floating village with houses , shops, and hotels on all kinds of boats.


    A night on a House Boat




Take a Shikara ride.Watch the super clear reflection of the mountain. Spot fishes and water birds.



Take a 2 day stay in Pahalgam. Visit Mini Switzerland first day on a horse ride.


Visit Sonemarg. Enjoy the snow.



On your way to Pahalgam, visit apple orchids . Taste all kind of apples. The golden ones are supposed to be the best.



The dark ominous mou…

Kerala : How to plan for a lifetime's experience

I have been planning this for months and it finally came along in the best time. A full stretch Kerala trip with Kanyakumari as an added bonus. I will not tell you what to do, but HOW to do it. How to plan your dream Kerala Trip. 

Caution: Mine was too comprehensive. Needs high energy and will be quite tiring. 

I chose the monsoon theme..I wanted to be in the middle of the action. This is the only place in India which offers monsoon as a tourism opportunity. I was lucky. I expected super heavy rains. Instead. I ended up right in between the rainy season and summer which was Perfect.

Advantages in travelling in mid AugustRainfall almost over.Waterfalls at their fullest.Greenery all over the state.Backwaters, beach sides and rivers all very clean.Abundant coconuts to enjoy.Hotel rates brought down.Less crowd everywhere.Festive Preparation going on.Shopping becomes pocket friendly.Our route map:






Day 1 Day 2 Day 3 Day 4 Day 5 Day 6

CNTLM in Office

If you are facing a proxy problem , CNTLM is a general option everybody considers. But the working of CNTLM is not always guaranteed. I myself get confused every time I need to use it.

What problem are we actually talking about?

While working in offices and restricted environments where firewalls and other invisible barriers stop your requests from going out, you sometimes need a way to temporarily jump over that barrier to get your job done . Example: You might face them while trying to install using pip in command line, trying to clone a personal git repository. 

What is CNTLM exactly?

I think this is best answered by the creator himself. Refer this. You can download it from here. For configuration, you an refer this. There are many uses of CNTLM but I am trying to solve my own problem here. So I will stick to breaking office barriers for now ;) .

Why do I need to use it :
1) Python package installations using PIP
2) Git operations : Cloning and pushing to server.

Here are a set of steps I …