On Dec 17, 10:00 pm, Brendan Miller <catph...@catphive.net> wrote: > On Thu, Dec 17, 2009 at 6:44 PM, Steven D'Aprano > > <st...@remove-this-cybersource.com.au> wrote: > > On Thu, 17 Dec 2009 12:07:59 -0800, Brendan Miller wrote: > > >> I was thinking it would be cool to make python more usable in > >> programming competitions by giving it its own port of the STL's > >> algorithm library, which needs something along the lines of C++'s more > >> powerful iterators. > > > For the benefit of those of us who aren't C++ programmers, what do its > > iterators do that Python's don't? > > Python iterators basically only have one operation: > > next(), which returns the next element or throws StopIteration. > > In C++ terminology this is a Input iterator. It is good for writing > "for each" loops or map reduce operations.
Hmm. I guess the main thing that's bothering me about this whole thread is that the true power of Python iterators is being overlooked here, and I don't think you're being fair to call Python iterators "weak" (as you did in another thread) or say that they are only good for for-else type loops. The fact is, Python iterators have a whole range of powers that C++ iterators do not. C++ iterators, at least the ones that come in STL, are limited to iterating over pre-existing data structures. Python iterators don't have this limation--the data being "iterated" over can be virtual data like an infinite list, or data generated on the fly. This can be very powerful. Here's a cool slideshow on what can be done with iterators in Python (generators specifically): http://www.dabeaz.com/generators/ It is true that Python iterators can't be used to mutate the underlying structure--if there is actual underlying data structure-- but it doesn't mean they are weak or limited. Python and C++ iterators are similar in their most basic usage, but grow more powerful in different directions. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list