O'Reilly logo

Python Essential Reference, Second Edition by David M Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required