A generator is the easy way to write an iterator. You write an ordinary-looking function, but wherever it should hand out a value, you write yield instead of return. Python turns the function into an iterator for you, and takes care of the position, of StopIteration and of everything else the last lesson did by hand. It is one of the most useful features in the language.
More lessons in Complete Python Programming