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: http://www.python-course.eu/python3_magic_methods.php.

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.

Presented by

Tennessee Leeuwenburg

Tennessee started coding when the internet did not exist. He is very glad the internet now exists and has been fascinated watching the growth in IT and related areas over the past 30 years. He can remember the first bug he ever had to fix - how to make his program save a copy of it's state, rather than a copy of it's source code.

Tennessee has moved from help desk operator (have you tried turning it off and on again?), through the developer ranks (is it definitely plugged in?) and now manages a team of around twenty (what exactly does IT stand for?).

He really likes Python. He also really likes attending and giving presentations at PyCon AU.


Sponsors