This is an old copy of the Python FAQ. The information here may be outdated.

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


A Django site. rendered by a django application. hosted by webfaction.