6 ms·
How Python attributes work
- r4victor 6y agoHi! 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. I welcome your feedback and questions. Thanks!
- alexpetralia 6y agoJust wanted to say I found your series incredibly valuable! Thank you.
- rytill 6y agoYou are providing immense valuable to your target audience. I would love to see an article about threading and asyncio.
- rytill 6y agoWhat are some of your favorite projects built with Python?
- r4victor 6y agoPelican. It took me an hour or two to set up this blog. Intuitive, flexible and well-documented.
- miguendes 6y agoAnother awesome article! Really enjoy going through them, I've learnt tons about the internals of Cpython.
- m12k 6y agoInteresting 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"
- joombaga 6y agoAs did I! Rephrasing the title as a question avoids this peculiarity.
- r4victor 6y agoSorry 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.
- mallocq 6y agoThat 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.
- pansa2 6y ago> 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: https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-to-see/ https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-t...
- ashconnor 6y agoThere's a great on this by the same author: https://www.youtube.com/watch?v=qCGofLIzX6g https://www.youtube.com/watch?v=qCGofLIzX6g