How dunder methods rule Python under the hood
- Level:
- beginner
- Room:
- south hall 2a
- Start:
- Duration:
- 30 minutes
Abstract
Python dunder methods – like __init__
– are sometimes referred to as “magic methods” but they are not!
They are just regular methods!
Functions that are associated with objects and that you can call with arguments.
The only thing is... Python also calls those functions behind the scenes in certain situations!
So, let us learn what that is all about.
Description
In this talk aimed at beginners, we will see what “dunder methods” are and how they govern Python behind the scenes.
We will understand, for example, why the funny-looking method __init__
looks so funny and what its real purpose is.
Then, we will understand that all of the Python syntax really is governed by dunder methods, and thus, if you want your own classes and objects to interact with the Python syntax, all you need to do is implement the appropriate dunder methods.
For example, we will see how to use dunder methods to enable you to create a good string representation of your class, or how to define len
on your own objects.
Although the talk is aimed at beginners, the audience is expected to know how to use the keyword class
to create their own classes.
All materials can be accessed from this GH repo.