Magic methods (formally known as special methods) are:
[...] called implicitly by Python to execute a certain operation on a type, such as addition. Such methods have names starting and ending with double underscores.
5 line example:
>>> class MyObj:
... def __add__(self, other):
... print(self, other) # return something sensible
...
>>> MyObj() + 42
<__main__.MyObj object at 0x7f3637f8ea90> 42
- Slides
- Notebook
- Notebook: © Nick Timkovich, All rights reserved. Licensed under the MIT License.
- Slide text, figures (excluding trademarks and logos), and source listings, excluding style elements © Nick Timkovich, All rights reserved. Licensed under CC-BY-SA 4.0
- Typeset slides and styles: © Telnyx, LLC, All rights reserved. Licensed under CC-BY-NC-ND 4.0.
- Duck cutout is a derivative of a photo by Bengt Nyman, and licensed under CC-BY-SA 4.0