#
3.3 Special method names

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python's approach to operator overloading, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named `__getitem__()`, and `x` is an instance of this class, then `x[i]` is equivalent to `x.__getitem__(i)`. Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined.

When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the `NodeList` interface in the W3C's Document Object Model.)

Subsections

- 3.3.1 Basic customization
- 3.3.2 Customizing attribute access
  - 3.3.2.1 More attribute access for new-style classes
  - 3.3.2.2 Implementing Descriptors
  - 3.3.2.3 Invoking Descriptors
  - 3.3.2.4 __slots__
- 3.3.3 Customizing class creation
- 3.3.4 Emulating callable objects
- 3.3.5 Emulating container types
- 3.3.6 Additional methods for emulation of sequence types
- 3.3.7 Emulating numeric types
- 3.3.8 Coercion rules