By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

iterators

P: n/a
Hi.

Python support forward iterators, but what about bidirectional and
random access ones?
Regards Manlio Perillo
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Manlio Perillo wrote:
Python support forward iterators, but what about bidirectional and
random access ones?


'random access iterators' are called 'Sequence' types in Python
(http://www.python.org/doc/2.3.3/lib/typesseq.html). You can create your own
sequence types by implementing some special methods
(http://www.python.org/doc/2.3.3/ref/...ce-types.html).

Daniel

Jul 18 '05 #2

P: n/a
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
"Manlio Perillo" <NO******************@libero.it> wrote in message
news:o9********************************@4ax.com...
Hi.

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

Jul 18 '05 #3

P: n/a

"Manlio Perillo" <NO******************@libero.it> wrote in message
news:o9********************************@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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.