for iteration
Gerhard Häring
gh at ghaering.de
Fri Aug 22 17:04:21 EDT 2003
Franck Bui-Huu wrote:
> Hello,
>
> I'm trying to customize a list by overriding __getitem__ method but
> this change seems to not work with for iteration.
> When I use my customized list in a for iteration, all changes made
> in __getitem__ are not take into account.
> How can I modify this behaviour ?
To make iteration via "for" possible, the recommended way is to
implement the iterator protocol in your object, i. e. the object itself
has a method "__iter__(self)" that will return an iterator object.
Usually the iterator object is the object itself! The iterator object
has one method, called "__next__(self)" that will return the next object
or raise StopIteration when there are no further objects.
For your particular problem, though, please post code to show it.
-- Gerhard
More information about the Python-list
mailing list