Possible Duplicate:
What can you use Python generator functions for?
I tried to read about python generators but did not understand much about the concept as to what we can do with generators, I am new to python
please let me know Thank you
Possible Duplicate:
What can you use Python generator functions for?
I tried to read about python generators but did not understand much about the concept as to what we can do with generators, I am new to python
please let me know Thank you
The presentation here explains generators very well:
http://www.dabeaz.com/generators/index.html
I have yet to find a use for the more advanced pipelining stuff, but I use the general technique all the time to parse logfiles.
While Yassin's answer is completely correct, I would rather explain it differently: A generator is a function that returns multiple values over time, where each value is generated (and returned) when you ask for it.
http://docs.python.org/tutorial/classes.html#generators Read this first.
Basically, generators are iterable objects. The magic word here is yield. Instead of using the return statement, you use yield, which doesn't stop the execution of a function, but returns something. In order for you to be able to consume what the generator returns, you have to iterate through it.