PyCon Sweden 2025

The Magic of Self: How Python inserts self into methods
2025-10-30 , Auditorium

Chances are that you’ve noticed that Python magically inserts “self” into methods for you. What you might not know is that the power behind this magic is the well-defined descriptor protocol. In fact, descriptors power many of the magical things in Python, from simple properties to lazy-loading attributes on database models.

In this talk, I will explain the magic of self by introducing you to the descriptor protocol. I will show you how this protocol powers the insertion of self and what else you can do with it. By the end, you will have a solid understanding of descriptors and know enough to start implementing descriptors of your own.


This talk explores the "magic" behind Python's automatic insertion of self into methods. While beginners are told "don't worry, it just happens" and experienced developers stop noticing it, there's fascinating machinery underneath.

I will start my talk by showing the automatic insertion of self in action. I will show you that we get an argument "for free" when calling a method: despite only passing a single argument, our method actually receives two values!

After showing you the Magic of Self in action, we will discuss why this happens. No, it's not because we named one of the parameters self when we defined the method. No, it's not even the fact that we defined a function within the body of a class. In fact, the magic happens when we access the method.

As it turns out, accessing attributes (including those that are bound to methods) isn't as simple as it looks at a glance. We can actually influence what happens when we access attributes by implementing something called a descriptor. That's why I will continue by introducing you to the descriptor protocol and showing you how to implement a descriptor of your own.

Now that we know how descriptors work, we're finally ready to understand the magic of self. To understand it, we now turn our attention to functions and observe that all functions are, in fact, such descriptors.

I will conclude my talk by providing a few examples of other descriptors that you may find in Python and the standard library.

Sebastiaan is a Python enthusiast who enjoys thinking about good software design and engineering. He is a fellow of the EuroPython Society and the Python Software Foundation, works for the Sopra Steria Pythoneers, and frequently speaks at conferences around the world.