Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Kent Johnson
Marcus Goldfish wrote: I see that an iterator is conceptually distinct from the container object it iterates over, but I am confused that both the iterator and container implement __iter__() to support the iterator protocol. I think this is to simplify the Python runtime. 'for i in c' will work if

Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Kent Johnson
Sending to the list, originally this went just to Marcus... Marcus Goldfish wrote: 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__"""

Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Marcus Goldfish
> - As Rich pointed out, making Foo into it's own iterator by adding a next() > method is not a good idea. A simple way to define your own iterator is to I see that an iterator is conceptually distinct from the container object it iterates over, but I am confused that both the iterator and containe

Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Rich Krauter
Kent Johnson wrote: Rich Krauter wrote: 2) Or, if you really want your __eq__ method to use the iterator returned by __iter__(), def __eq__(self, other): for i, j in map(None,self, other): if i != j: return False return True That's not right either, it will compare Fo

Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Kent Johnson
Rich Krauter wrote: 2) Or, if you really want your __eq__ method to use the iterator returned by __iter__(), def __eq__(self, other): for i, j in map(None,self, other): if i != j: return False return True That's not right either, it will compare Foo([None], []) == Foo(

Re: [Tutor] iterator question for a toy class

2005-04-23 Thread Rich Krauter
Marcus Goldfish wrote: 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.tai

[Tutor] iterator question for a toy class

2005-04-22 Thread Marcus Goldfish
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 de