Conditionally implementing __iter__ in new style classes
Leif K-Brooks
eurleif at ecritters.biz
Wed Jul 6 12:35:10 EDT 2005
Thomas Heller wrote:
> I forgot to mention this: The Base class also implements a __getitem__
> method which should be used for iteration if the .Iterator method in the
> subclass is not available. So it seems impossible to raise an exception
> in the __iter__ method if .Iterator is not found - __iter__ MUST return
> an iterator if present.
def Iterator(self):
for index in xrange(len(self)):
yield self[index]
def __iter__(self):
return self.Iterator()
...and then override Iterator in subclasses. But this raises the
question of why you need to use a specially-named method instead of
having subclasses override the __iter__.
More information about the Python-list
mailing list