We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

What is a sequence?

A sequence is an informal interface for container objects that hold zero or or more values, in a given order. A sequence object must support the object[i] syntax, and should usually have a known length. Sequences may also support slicing and other list-style methods.

The standard str, unicode, list, and tuple types are all sequence objects.

To implement your own sequence class, you must implement a __getitem__ method, and may also a __len__ method. The __getitem__ method must raise an IndexError exception for invalid indexes. An example:

class StringRange:
    def __len__(self):
        return 10
    def __getitem__(self, index):
        if index >= len(self):
            raise IndexError
        return str(index)

CATEGORY: general

CATEGORY: programming