Comment on PEP-0322: Reverse Iteration Methods

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Sun Sep 28 19:51:03 CEST 2003


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. 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? 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.

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 - 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.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list