Comment on PEP-0322: Reverse Iteration Methods

David Abrahams dave at boost-consulting.com
Sun Sep 28 19:35:41 EDT 2003


Stephen Horne <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> writes:

> On Sun, 28 Sep 2003 11:22:11 -0400, David Abrahams
> <dave at boost-consulting.com> wrote:
>
>>Stephen Horne <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> writes:
>>
>>>>Sorry, what specialization?
>>>
>>> Presumably template specialisation - such that the special case of
>>> std::pair<T,T> picks up the iterating functionality that
>>> std::pair<T,U> lacks (begin, end etc). That is what I thought you were
>>> saying.
>>>
>>> Or am I still getting this wrong?
>>
>>Yeah, slightly.  You don't need a begin() member function in order to
>>make an iterator.  The interface might look like:
>>
>>  std::for_each(pair_iterator<T>(my_pair), pair_iterator<T>(), f);
>>
>>Decoupling is the way to go, man! :^)
>
> Ah - I get it! - std::pair doesn't exactly support iteration itself,
> but a support class can be used to add that capability.
>
> You can do this in any language. 

Yes; I never implied otherwise.

> For instance, did you know that Python classes supports iterating
> through the subset of their attribute that have names beginning with
> "a", interleaved with insults? 

I never, anywhere, said "std::pair supports..."

> Yes, all you need is to use this
> generator...
>
>   def A_Attrib_Gen (p_Instance) :
>     for i in dir (p_Instance) :
>       if i[0] = "a" :
>         yield i
>         yield "stupid stupid stupid"
>
> Well, OK, maybe this is a little unfair - this generator isn't exactly
> in the library, but with a little luck you see my point.

Your sarcasm is briefly amusing, but unwarranted.

> When you say "you can in fact iterate on std::pair<T,T> with the usual
> C++ iterator protocol" it implies to me that std::pair<T,T> provides
> the iterator protocol itself

Hey, it's not my fault you read what you want to see into what I
posted.

>  - not that some other class provides a
> way to support iteration over the pair. After all, there is *always*
> some way to support iteration of *anything*.
>
> But maybe I'm just being overliteral.

No, you just don't know what "the usual iterator protocol" means.  It
has to do with the protocol for using iterators, and begin()/end() are
nowhere in the iterator requirements.  They're part of the container
requirements.  There are many iterators that have nothing to do with
containers and have never been passed through a begin()/end().
Pointers to built-in arrays are one obvious example.

This whole discussion started out talking about what the Python
protocol for manipulating iterators should be.  The fact that
iterators must themselves have an __iter__ method in Python may just
be making this more confusing than it needs to be.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Python-list mailing list