467,077 Members | 964 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Implementing Iterators in, e.g., Linked Lists

I am taking a second programming course in Java and am currently
attempting to apply what I have learned using Python instead. One
thing that is puzzling me is how to use an iterator.

I am writing a module containing everything I'd need for canonical
linked lists. One particularly useful feature would be to use a for
loop over the whole structure. For this I have learned the benefits of
iterators. I have read a few book entries on Python iterators, as well
as an online article by David Mertz, I believe, and PEP 234, and I may
be lacking some insight but I am still confused about one thing. How
does the iteration know where to begin?

AFAIU, in my LinkedList class, I can either have the LinkedList be its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?

My problem with implementing the former comes with this: in LinkedList
I would have:

....

def __init__(self):
return self

....

def next(self):
if self.__current.next == None:
raise StopIteration
self.__current = self.__current.next
return self.__current.next

....

Now, is this good in the eyes of more experienced programmers? Also,
do I really want to dedicate an instance variable to keep track of
where to begin iteration (if __current is used for other purposes,
iteration could conceivably begin anywhere right?)? Does this suggest
that I should have a separate LinkedListIterator class? And if I do
have that separate class, do I make the LinkedList.__iter__() pass on
to LinkedListIterator's constructor the head node of LinkedList, or
the whole linked list?

Thanks in advance,

Jeremy
Jul 18 '05 #1
  • viewed: 1518
Share:
1 Reply
adeleinandjeremy wrote:
AFAIU, in my LinkedList class, I can either have the LinkedList be its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?


External iterators are always preferable for two reasons.

1. They allow multiple iterators over the same collection.

2. They keep the interface of the base object clean by separating other
functionality.
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Jess Austin | last post: by
7 posts views Thread by sks_cpp | last post: by
4 posts views Thread by jhonyxxx | last post: by
24 posts views Thread by Lasse Vågsæther Karlsen | last post: by
51 posts views Thread by Joerg Schoen | last post: by
2 posts views Thread by Thormod Johansen | last post: by
9 posts views Thread by nottheartistinquestion@hotmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.