Iterator for Custom Sequence
Denis S. Otkidach
ods at fep.ru
Tue Jul 16 07:27:17 EDT 2002
On 16 Jul 2002, Patrick W wrote:
PW> To learn how to provide iterators for custom sequences, I've
PW> made a
PW> typical linked list with the following __iter__ method:
PW>
PW> class LinkedList:
PW> ....
PW> def __iter__(self):
PW> def genitems(ls):
PW> n = ls.head
PW> while n.data is not None:
PW> yield n.data
PW> n = n.next
PW> return genitems(self)
PW> ....
__iter__ method can be implemented as generator itself:
class LinkedList:
...
def __iter__(self):
n = self.head
while n.data is not None:
yield n.data
n = n.next
...
--
Denis S. Otkidach
http://www.python.ru/ [ru]
http://diveinto.python.ru/ [ru]
More information about the Python-list
mailing list