iterators

M

Manlio Perillo

Hi.

Python support forward iterators, but what about bidirectional and
random access ones?


Regards Manlio Perillo
 
G

gsm

Not too sure but I suppose you can design class iterators and you decide how
they behave.

I normally just add a __iter__ method and use yield

class IteratorObject:

def __init__(self, data):

self.data = data
self.__items = len(data)

def __iter__(self):

index = 0

while index < self.__items:

result = blah blah i.e do what ever you want here
#eg return a random item from list
yield result
index += 1

not sure if this helps
 
T

Terry Reedy

Manlio Perillo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top