Iterator - what I am missing

Helmut Jarausch jarausch at
Thu Aug 14 21:08:32 CEST 2003


I cannot find out what I miss with my iterator example.

I have a linked list class Queue which has a 'Head' attribute
and the following

     def __iter__(self):
         if  self.Head == None: raise StopIteration
         return self.Head

The elements of the list are of  class Node

which has a 'NNd' (next Node) attribute and
a next method

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

Now when I try to iterate over all elements of the
Queue like in  (MyQ is an instance of class Queue)

for N in MyQ:
     print "loop->",N
     print N.key,

I get an infinite loop and 'N' always refers to the first
element of the MyQ as initialized by  MyQ.__iter__

I've checked 'MyQ' and it contains 2 elements and 'NNd'
attribute of the last one is 'None'

So what's going on here?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

More information about the Python-list mailing list