Showing posts with label Object. Show all posts
Showing posts with label Object. Show all posts

Creating generators objects in python

Sunday, June 2, 2013

What is Generator?

A generator is a function which can stop whatever it is doing at an arbitrary point in its body, return a value back to the caller, and, later on, resume from the point it had `frozen' and merrily proceed as if nothing had happened (link) . Here is a simple example: 

if you write 

x = [n for n in foo if bar(n)]

you can get the list object assigned to the x.



x=(n for n in foo if bar(n))
you can get out the generator and assign it to x. Now it means you can do