TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How Python attributes work

103 点作者 r4victor超过 4 年前

4 条评论

r4victor超过 4 年前
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&#x27;ll study how Python attributes work. You&#x27;ll learn:<p><pre><code> - 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. </code></pre> I welcome your feedback and questions. Thanks!
评论 #25586148 未加载
评论 #25586485 未加载
评论 #25586547 未加载
m12k超过 4 年前
Interesting how that headline can be parsed in different ways, since &quot;attributes&quot; and &quot;work&quot; can both be nouns or verbs. I initially read it as &quot;How Python contributors are given credit for their work&quot;
评论 #25591210 未加载
评论 #25590060 未加载
pansa2超过 4 年前
&gt; <i>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.</i><p>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:<p><a href="https:&#x2F;&#x2F;lucumr.pocoo.org&#x2F;2014&#x2F;8&#x2F;16&#x2F;the-python-i-would-like-to-see&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lucumr.pocoo.org&#x2F;2014&#x2F;8&#x2F;16&#x2F;the-python-i-would-like-t...</a>
评论 #25589717 未加载
miguendes超过 4 年前
Another awesome article! Really enjoy going through them, I&#x27;ve learnt tons about the internals of Cpython.