Looking for doh!.. simple sequence type

Max M maxm at mxm.dk
Fri Apr 12 07:02:48 EDT 2002


Hmm ... I am trying to create a simple sequence type that I cant iterate 
over like (Python 2.1)::

lst = Lst()
for i in lst:
     print i

And I thought I could do it like:

class Lst:

     def __len__(self):
         return 4

     def __getitem__(self, idx):
         return 42

But I have to write it like below to avoid an endless loop..

lst = Lst()
for i in range(len(lst)):
     print i,

 >>>42 42 42 42

I am shure it's so simple tha I will go Doh! .. But perhaps I haven't 
had enough cofee yet. 'Cause I cannot seem to figure it out.

regards Max M




More information about the Python-list mailing list