Talk

Dipping my toes in metaprogramming

LanguageEnglish
Audience levelAdvanced
Elevator pitch

Everyone knows the dunder method __init__. But have you heard of __init__‘s big brother, __new__?

This live-coded 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!

Abstract

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 live-coded talk will use a progression of a series of small examples and analogies 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:

  • explore the functionality from pathlib.Path that automatically creates different instances based on the OS of the user;
  • understand how __new__ and __init__ are used in different moments of class instantiation;
  • see how the return value of __new__ interacts with the dunder method __init__; and
  • use __new__ to subclass immutable types and, in particular, to create a class TolerantFloat as a subclass of float.
TagsLanguage and features
Participant

Rodrigo Girão Serrão

Rodrigo Girão Serrão is the author of multiple independently published books on Python, programming, and mathematics, and frequently blogs about those same topics. Rodrigo has also presented talks and tutorials at some of the largest Python conferences in the world, including PyCon US, EuroPython, and multiple European PyCons.

At Polars, Rodrigo channels his pedagogical skills and his passion for teaching to make Polars more accessible to everyone. By creating technical content that is easily digestible by those looking to start using, or improve their usage of Polars, Rodrigo is working to help everyone appreciate the elegance and effectiveness of the Polars API.

When he is not working or writing, Rodrigo can be found playing board games, walking his dog, or exercising at the gym.