iterators

Terry Reedy tjreedy at udel.edu
Thu May 13 12:12:28 EDT 2004


"Manlio Perillo" <NOmanlio_perilloSPAM at libero.it> wrote in message
news:o996a0t6ra0pj42emgjpjsr9c375dracrf at 4ax.com...
> Hi.
>
> Python support forward iterators,

Actually, it supports general linear iteration, which is to say,
one-at-a-time processing of every item in any collection, however
structured, in whatever order.  Structured collections usually have special
orders like forward and backwards for sequences, alphabetic for string
collections, pre-order and post-order for trees,  and also in-order for
binary trees.  Support has three aspects: a definition of 'iterator-ness'
(having __iter__ and next methods -- the 'iterator protocol') that users
can implement, implementation of the protocol for all appropriate builtin
collection types as well as provision of two special interator types that
serve as return values for iter() and generator functions, and use of the
protocol by for statements.

< but what about bidirectional

List .push and .pop can be thought of as a type of bidirectional access.
More generally, one can augment .next with a .prev method, and people have.
This is specifically appropriate for a doubly-linked list, actual or
virtual.  If a collection can be randomly accessed (see below), then
bidirectional access is easily accomplished with a cursor that is
incremented and decremented, but there is usually no reason to limit index
changes to +-1.  For a multidimensional array, one could define a
corresponding multidirectional cursor.

> random access ones?

See __getitem__ methods and index (object[]) syntax, which apply to both
sequences and associations.  A random access cursor is a name bound to an
approprite index object or a collection object containing such.

Terry J. Reedy








More information about the Python-list mailing list