Under and Dunder - Python secret functions

By Cheuk Ting Ho

Elevator Pitch

You may know dunder init, but how well do you know about other Python “magic” functions? Why are dunder functions not accessible? On the other hand, internal functions that start with an underscore are accessible and I think that’s beautiful. Do you want to know why? Let me tell you all of these.

Description

You may know dunder init (__init__), but how well do you know about other Python “magic” functions? Why are dunder functions not accessible? Where are they hiding? What purpose do they serve? in this talk, I am going to show you the secrets of the Dunder functions and what purposes do they have. Once the mystery is solved, we will see the true beauty of those functions.


At the start of this adventure, we will be using dir to look into the secret functions Python objects have. Eg: __getitem__, __iter__ and __next__ Then, we will investigate where does the dunder function goes and the reason why is it not accessible externally. We will also find out how did Python treat them internally.

Once we get warmed up, we will have a deeper look. We will see by overloading these dunder methods, we can change the behaviour of the operator on Python objects. We will learn how operator overload is performed in Python. We will also showcase a few examples when this is useful when we want to write “beautiful” codes.

And in the end, we will see what other magics dunder methods could do. We will see how dunder functions can change the identity of the Python objects. Through some experiments and looking at the bytecodes, we will have a brief idea of how Python works internally.


This talk is for attendees who have just begin their Python journey. By explaining the dunder functions and the nature of Python objects, attendees will have a better understanding of why Python is fundamentally different from other programming languages. Hopefully, this knowledge will help them out when they continue their Python journeys.