The following methods can be defined to implement container
objects. Containers usually are sequences (such as lists or tuples)
or mappings (like dictionaries), but can represent other containers as
well. The first set of methods is used either to emulate a
sequence or to emulate a mapping; the difference is that for a
sequence, the allowable keys should be the integers k for which
0 <= k < N where N is the length of the
sequence, or slice objects, which define a range of items. (For backwards
compatibility, the method __getslice__() (see below) can also be
defined to handle simple, but not extended slices.) It is also recommended
that mappings provide the methods keys(), values(),
items(), has_key(), get(), clear(),
setdefault(), iterkeys(), itervalues(),
copy(), and update() behaving similar to those for
Python's standard dictionary objects.
Mutable sequences should provide
methods append(), count(), index(),
insert(), pop(), remove(), reverse()
and sort(), like Python standard list objects. Finally,
sequence types should implement addition (meaning concatenation) and
multiplication (meaning repetition) by defining the methods
__add__(), __radd__(), __iadd__(),
__mul__(), __rmul__() and __imul__() described
below; they should not define __coerce__() or other numerical
operators. It is recommended that both mappings and sequences
implement the __contains__() method to allow efficient use of
in operator; for mappings,
in should be equivalent
of has_key(); for sequences, it should search through the
values. It is further recommended that both mappings and sequences
implement the __iter__() method to allow efficient iteration
through the container; for mappings, __iter__() should be
the same as iterkeys(); for sequences, it should iterate
through the values.
>=0. Also, an object that doesn't define a __nonzero__() method and whose __len__() method returns zero is considered to be false in a Boolean context.
self[key]. For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the __getitem__() method. If key is of an inappropriate type, TypeError may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), IndexError should be raised. Note: for loops expect that an IndexError will be raised for illegal indexes to allow proper detection of the end of the sequence.
self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper key values as for the __getitem__() method.
self[key]. Same note as for __getitem__(). This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper key values as for the __getitem__() method.
Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see ``Iterator Types'' in the Python Library Reference.
The membership test operators (in and not in) are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence.
See About this document... for information on suggesting changes.