Next: Special methods for Up: Special method names Previous: Special methods for

Special methods for sequence and mapping types

__len__(self)
Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object whose __len__() method returns 0 is considered to be false in a Boolean context.

__getitem__(self, key)
Called to implement evaluation of self[key]. Note that the special interpretation of negative keys (if the class wishes to emulate a sequence type) is up to the __getitem__ method.

__setitem__(self, key, value)
Called to implement assignment to self[key]. Same note as for __getitem__.

__delitem__(self, key)
Called to implement deletion of self[key]. Same note as for __getitem__.


guido@cwi.nl