Special Methods

All the built-in datatypes consist of some data and a collection of special object methods. The names of special methods are always preceded and followed by double underscores (_ _). These methods are automatically triggered by the interpreter as a program executes. For example, the operation x + y is mapped to an internal method x ._ _add_ _ (y ), and an indexing operation x [k ] is mapped to x ._ _getitem_ _ (k ). The behavior of each datatype depends entirely on the set of special methods that it implements.

Although it’s not possible to alter the behavior of built-in types (or even to invoke any of their special methods directly by name, as just suggested), it’s possible to use class definitions to define new objects that ...

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.