for item in some_class_object...

Sean Blakey sblakey at freei.com
Fri Apr 14 18:51:39 EDT 2000


On 14 Apr, Ben Wolfson wrote:
> 
> What special methods should I define for a class if I want to support
> a for item in ... syntax for it?
> 
All you need to do is implement __getitem__.

>>>class Seq:
...    def __getitem__(self, i):
...        if i < 10:
...            return i
...        else:
...            raise IndexError
...
>>>s = Seq()
>>>for i in s:
>>>    print i

-- 

Sean Blakey
FreeInternet.com
sblakey at freei.com
(253)796-6500x1025





More information about the Python-list mailing list