How Python attributes work
tenthousandmeters.com
tenthousandmeters.com
- 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.
I welcome your feedback and questions. Thanks!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:
https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-t...