Comment on PEP-0322: Reverse Iteration Methods
David Abrahams
dave at boost-consulting.com
Sat Sep 27 08:07:11 EDT 2003
Alex Martelli <aleax at aleax.it> writes:
> David Abrahams wrote:
> ...
>>> I always assumed that these were considered inconsistent with normal
>>> use of tuples (which certainly I rarely need to get 'index' or
>>> 'count'-like results from).
>>
>> I don't see why not.
>
> The party line (not saying I necessarily agree, mind you -- just relating
> it) is that tuples are meant as semantically heterogeneous containers
> (semantically in that the items may happen to be the same type, but
> their _meaning_ is disparate: e.g., consider the tuples used in modules
> time or stat). So it doesn't really make sense to iterate on them, either;
> it just happens to be possible because their items are accessed with []
> with progressive naturals, and they have a len(). Associating _names_
> to the indices makes more sense and indeed such pseudo-tuples are
> starting to be supplied by the standard library (though not a general
> mechanism for making your own, yet). Much like you can't iterate in
> C++ on a std::pair<>, thus you "shouldn't" be able to iterate on a
> tuple.
Well, (understanding that you don't nececessarily agree with the
above) you can in fact iterate on std::pair<T,T> with the usual C++
iterator protocol, and with a new mixed compile-time/runtime tuple
iterator protocol developed by Doug Gregor, iteration over
heterogeneous tuples is possible too. It certainly is desirable to be
able to do that; it's a real need that has come up in practice.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Python-list
mailing list