Iterator - what I am missing
mpeuser at web.de
Thu Aug 14 22:38:28 CEST 2003
the information you give is *very* imcomplete ;-) Probably the *next* method
is in the wrong class (Node instead of Queue). But one can only help with
the full code of both classes...
Have you considered using the new generators? They are not as new (from
__future__ import generators) if you will not use Python 2.3. They make
iterators much more transparent!
"Helmut Jarausch" <jarausch at skynet.be> schrieb im Newsbeitrag
news:3f3bde30$0$297$ba620e4c at reader1.news.skynet.be...
> 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,N.data
> 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