Everyone knows the dunder method __init__.
But have you heard of __init__‘s big brother, __new__?
This talk shows you what the dunder method __new__ does and how it works.
Join me and dip your toes in the world of metaprogramming in Python!
Metaprogramming can be quite an intimidating field and the truth is that most of the time you don’t need to know much about it. If anything at all.
The dunder method __new__ lives in the border between esoteric metaprogramming you’d learn “just because” and the Python features that advance your understanding of the language and make you a better developer.
This talk will use a progression of a series of small examples and live-coded demos to take you from the dunder method __init__, that you should be familiar with, to the method __new__.
By the end of the talk, you will understand how __new__ fits within the Python data model and what it can be useful for.
To reach this understanding, we will:
pathlib.Path that automatically creates different instances based on the OS of the user;__new__ and __init__ are used in different moments of class instantiation;__new__ interacts with the dunder method __init__; and__new__ to subclass immutable types and, in particular, to create a class TolerantFloat as a subclass of float.