subclassing list

spike spike.666 at
Mon Aug 1 01:19:25 CEST 2005

I've googled like crazy and can't seem to find an answer to why this
isn't working.

I want to create a custom list class that acts as a circular list.

ie: my_list = (0, 1, 2)

how I want it to behave:

my_list[0] -> 0
my_list[1] -> 1
my_list[2] -> 2
my_list[3] -> 0
my_list[4] -> 1

so, what I've tried to do is:

def circular_list(list):
    def __getitem__(self, i):
        if (i >= len(self)):
            return self[i % len(self)]
            return self[i]

items = circular_list(range(8))

however, when I want to iterate over it with a for statement, I get:

TypeError: iteration over non-sequence

what am I missing?


More information about the Python-list mailing list