iterators and views of lists

Alf P. Steinbach alfps at start.no
Fri Dec 18 14:08:27 EST 2009


* Carl Banks:
> 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.

It's good that Python iterators can do things.

However, it's not the case that C++ iterators can't do those things.

C++ iterators very much routinely do such things.

However, C++ iterators are flawed in a way that Python iterators are not.

You might say, in an analogy with control structures, that this flaw gives C++ 
iterators the power of "goto" but also with all the negative baggage...

I'm too lazy to Google, but you might search for Alexandrescu and "ranges", and 
possibly throw in "iterators" among the search terms.


Cheers & hth.,

- Alf



More information about the Python-list mailing list