Hi! This is part 7 of my Python behind the scenes series. Each part of the series covers how some aspect of the language is implemented in the interpreter, CPython. This time we'll study how Python attributes work. You'll learn:
- What CPython does to execute statements like value = obj.attr and obj.attr = value.
- Why attributes of different objects work differently.
- How attributes of most objects work.
- How the __getattribute__(), __getattr__(), __setattr__() and __delattr__() special methods customize attribute access, assignment and deletion.
- How built-in types get their attributes, e.g. __dict__, __dir__(), __base__. What these attributes are.
Interesting how that headline can be parsed in different ways, since "attributes" and "work" can both be nouns or verbs. I initially read it as "How Python contributors are given credit for their work"
That would be a short essay. The answer is: Rarely, unless you belong to the old boys club or work on the same projects as GvR or have the correct political opinions.
Sorry for being misleading. I didn't think about the other interpretation. It sounds intriguing, by the way. I'm thinking I'd like to learn more about it.
> For example, if we define the __add__() special method on an existing class, it will set the nb_add slot of the class to the default implementation that calls the method.
See also this post from 2014, which argues that this is a bad design. It claims that Python would be both simpler and faster if it looked up special methods like `__add__` by name every time, rather than using the slot mechanism: