Contribute Media
A thank you to everyone who makes this possible: Read More

Syntax Hijacking: Genius or Evil?


Many Python developers will be familiar with Python's __dunder__ methods, which can be used to define how classes are intended to operate according to various infix operators. For example, the developer can easily control what happens when an object interacts with '+', '-', '[]'. A full list can be found here:

This is used by some frameworks in a sympathetic way, such as matrix multiplication and addition, where the concepts extend very naturally to those classes. It can also be used for more advanced string and natural language processing, where the connection may be less clear. It is also possible to create utter confusion and havoc.

What is the difference between elegant re-use, and a violation of standard expectations? Should you avoid re-implementing these methods so that basic operators always have reliable semantics, or is it reasonable to create your own object semantics for a domain specific problem?

This talk will present some of the ways in which the magic methods can be used for ends either wonderful or terrible.


Improve this page