views:

81

answers:

4

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

+1  A: 

Simply put, a generator in Python is a function that can maintain state between values produced. Read this.

Yassin
+1  A: 

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.

AHM
+1  A: 

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.

poke
Why the downvote?
poke
+1 for the downvote
Yassin
A: 

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.

dekomote