Iterators and Generators and Yields

2012 May 12 at 03:04 » Tagged as :python, ruby, certification, threads, yield,

Both python and ruby have a yield which is thoroughly confusing to Java programmers and looks like the stuff that nightmares are made of. But eventually you realize that it isn't so bad after all. But before we deal with our nightmares, we to look at iterators.

Python for loops look so nifty thanks to iterators. Things like lists and tuples implement the iterator interface.  Of course in python you don't need explicit interfaces like what we are used to in Java. However a class that implements def __iter__(self): and def next(self): can be iterated over. Here is a complete example from the python tutorial.