Special methods

Special methods are an important part of Python. They are used to implement functionality that is common among various classes.

Special methods used in any class

__init__(self, *args, **kwargs) called at object creation with the arguments given in the constructor
__slots__ not actually a method, but a field which defines the object attributes, saving memory

Special methods used in container classes

__len__(self) called by len(container)
__getitem__(self, key) called by accesses to container[key]
__setitem__(self, key, value) called by "container[key] = value"
__delitem__(self, key) called by "del container[key]"
__iter__(self) called by iter(container)
__contains__(self, item)* called by test "item in container"
* If __contains__ is not defined, iteration through the container is performed, comparing each element to item and returning True as soon as there is a match, or False if no matches are found

And more...

Python recognizes a bunch of special methods that I didn't mention. See the language reference for more information.

Defining __iter__