Hacker News new | past | comments | ask | show | jobs | submit login
How Python attributes work (tenthousandmeters.com)
103 points by r4victor on Dec 30, 2020 | hide | past | favorite | 12 comments



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.
I welcome your feedback and questions. Thanks!


Just wanted to say I found your series incredibly valuable! Thank you.


You are providing immense valuable to your target audience. I would love to see an article about threading and asyncio.


What are some of your favorite projects built with Python?


Pelican. It took me an hour or two to set up this blog. Intuitive, flexible and well-documented.


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.


As did I! Rephrasing the title as a question avoids this peculiarity.


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:

https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-t...


There's a great on this by the same author: https://www.youtube.com/watch?v=qCGofLIzX6g


Another awesome article! Really enjoy going through them, I've learnt tons about the internals of Cpython.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: