iterators and views of lists

Brendan Miller catphive at catphive.net
Fri Dec 18 16:10:28 EST 2009


On Fri, Dec 18, 2009 at 10:39 AM, Carl Banks <pavlovevidence at gmail.com> wrote:
> On Dec 17, 10:00 pm, Brendan Miller <catph... at catphive.net> wrote:
>> On Thu, Dec 17, 2009 at 6:44 PM, Steven D'Aprano
>>
>> <st... at 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.
>

When I said they are "weak" I meant it in sense that the algorithms
writeable against an InputerIterator interface (which is what python's
iterator protocol provides) is a proper subset of the algorithms that
can be written against a RandomAccessIterator interface. The class of
algorithms expressible against a python iterator is indeed limited to
those that can be expressed with a for each loop or map/reduce
operation.

Yes, python iterators can indeed iterate over infinite lists. All that
you need to do is have the next() operation never throw. The same
thing can be done in c++, or any other language that has iterators.

Generators provide a convenient way to write input iterators; however,
the same thing can be done in any language. It just requires more
boilerplate code to keep track of the iteration state.

Of course, that's not to say generators aren't important. They make it
that much easier to write your own iterators, so in a practical sense,
people are more likely to write their own iterators in python than in
C++.



More information about the Python-list mailing list