Iterator for Custom Sequence

Patrick W csposting at yahoo.com.au
Tue Jul 16 07:15:55 EDT 2002


Hi folks.

To learn how to provide iterators for custom sequences, I've made a
typical linked list with the following __iter__ method:

class LinkedList:
    ....
    def __iter__(self):
        def genitems(ls):
            n = ls.head
            while n.data is not None:
                yield n.data
                n = n.next
        return genitems(self)
    ....

This does the job, but is it eccentric or counter-Pythonic?  If so,
what's the conventional way to provide an iterator for a custom
sequence?



More information about the Python-list mailing list