Iterator - what I am missing

Peter Otten __peter__ at web.de
Sat Aug 16 04:11:11 EDT 2003


Terry Reedy wrote:

> def __iter__(self):
>     current = self.Head
>     while 1:
>         if current == None: raise StopIteration
>         yield current
>         current = current.NNd()
> 

This can be simplified:

<untested>

def __iter__(self):
    current = self.head # uppercase attributes are ugly
    while current is not None:
        yield current
        current = current.nextNode() # no crptc abrvtns pls, Helmut :-)

</untested>

The StopIteration exception is raised implicitely at the end of the method 

Peter




More information about the Python-list mailing list