defining __getitem__

Gang Seong Lee gslee111 at
Thu Apr 27 02:59:33 CEST 2000

I'd like to have a method __getitem__ in a class which acts like other
sequence type.
For example,
class data:
    def __init__(self, n):
        self.maxsize = n

    def __getitem__(self, k):
        if k >= self.maxsize:
              # I want to stop here
              # ????
        return get_data_from_some_where_else(k)

d = data(10)
for el in d:
    print el
I want to make it stop at some point automatically, like range() does. I
don't want to use try: exception clause for this. Is there any way for this?


Gang Seong

More information about the Python-list mailing list