A Standard
Andrew Koenig
ark at research.att.com
Tue May 27 12:34:25 EDT 2003
Greg> Andrew Koenig wrote:
>> For example, in C++, every random-access iterator is also a forward
>> iterator. Accordingly, every algorithm that operators on forward
>> iterators will also operate on random-access iterators.
Greg> And in Python, every sequence (i.e. something that
Greg> implements __getitem__) can be iterated over, so
Greg> every algorithm that operates on iterables will
Greg> also operate on sequences.
You mean it is impossible for a user-defined type to implement
__getitem__ without also implementing __iter__?
Greg> Seems to me the concepts are quite similar, it's
Greg> just that the terminology is slightly different.
Greg> STL Python
Greg> -------------------------- ------------
Greg> random-access iterator sequence
Greg> forward iterator iterable
There is a key difference: A C++ forward iterator is a first-class
value, which means that you can copy it and use it again later to
iterate over the same sequence a second time. In general, you can't
do that with a Python iterator. Moreover, you can modify the values
of the elements of the sequence to which a forward iterator refers
(unless the elements themselves are of a type the prevents
modification).
Therefore a C++ forward iterator allows more operations than Python
iterators. Python iterators are much closer to C++ input iterators
than they are to forward iterators.
--
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark
More information about the Python-list
mailing list