I'm trying to understand custom iterators in Python, and created the
following toy class for sequence comparison, which seems to work:

class Foo(object):
   """A toy class to experiment with __eq__ and __iter__"""
   def __init__(self, listA, listB):
      self.head, self.tail = listA, listB
   def __iter__(self):
      return iter(self.head[:]+self.tail[:])
   def __eq__(self, other):
      """Foo instances are equal if their respective subsequences,
      head, tail, are in the same order"""
      diff = [i for i, j in zip(self, other) if i != j]
      return len(diff) == 0

>>> f1 = Foo( [1,2], ['a','b','c'] )
>>> f2 = Foo( [1,2], ['a','b','c'] )
>>> f3 = Foo( [1,2], ['a','b','d'] )
>>> f1 == f2, f1 == f3
(True, False)


I'm not really sure if I'm implementing iter() correctly, for
instance: should I make copies of the sublists?  Should I try to
implement my own next() method?

Advice, comments, and links to good tutorial web pages on iterators
are appreciated!

Thanks,
Marcus
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to