Implementing Iterators in, e.g., Linked Lists

Rainer Deyke rainerd at eldwood.com
Mon Dec 1 08:45:27 CET 2003


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 - rainerd at eldwood.com - http://eldwood.com






More information about the Python-list mailing list