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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

How the Python object system works

336 点作者 r4victor超过 4 年前

9 条评论

r4victor超过 4 年前
Hi! This is part 6 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 focus on the Python object system. You&#x27;ll learn:<p><pre><code> - What Python objects and types are and how they are implemented. - What slots are and how they determine the behavior of objects. - How slots are related to special methods. </code></pre> I&#x27;d be glad to get your feedback and answer your questions. Thanks!
评论 #25385386 未加载
评论 #25390073 未加载
评论 #25389711 未加载
评论 #25390241 未加载
评论 #25391689 未加载
pansa2超过 4 年前
Python&#x27;s descriptor protocol is very elegant. Given:<p><pre><code> class C: def __init__(self): self.a = 1 def b(self): print(f&#x27;b({self})&#x27;) @property def c(self): print(f&#x27;c({self})&#x27;) o = C() </code></pre> The descriptor protocol is a generic way to enable `o.a` to access an attribute of `o`, `o.b` to access an attribute of `C` and bind `self` to `o`, and `o.c` to access an attribute of `C`, bind `self` to `o`, <i>and call it</i>.<p>Do any other dynamic languages use a similar protocol for attribute access?<p>Ruby, for example, doesn&#x27;t need to - it can use a simpler approach because `o.x` never refers to an attribute of `o`.<p>JavaScript&#x27;s solution is less elegant - the way it sets the value of `this` is infamous, and AFAIK properties are treated as a special case rather than built on top of a generic protocol.
评论 #25396715 未加载
pfalcon超过 4 年前
As a shameless plug, here&#x27;s a proposal on how to optimize some things in Python object system: <a href="https:&#x2F;&#x2F;github.com&#x2F;pycopy&#x2F;PycoEPs&#x2F;blob&#x2F;master&#x2F;StrictMode.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pycopy&#x2F;PycoEPs&#x2F;blob&#x2F;master&#x2F;StrictMode.md</a> . Implemented in my lightweight Python dialect, <a href="https:&#x2F;&#x2F;github.com&#x2F;pfalcon&#x2F;pycopy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pfalcon&#x2F;pycopy</a>
choeger超过 4 年前
Very interesting and quite ... instructive. The lack of optimizations is interesting. One would naively imagine that treating, e.g., ints, strings, dicts, and lists special would gain some performance.
评论 #25389867 未加载
评论 #25389764 未加载
评论 #25389755 未加载
评论 #25389687 未加载
miduil超过 4 年前
Thank you so much for this article, I remember seeing some talk on this topic many years ago but could never find a comprehensive summary so far (or even a video of the talk).<p>I&#x27;d be really curious about this in other languages as well, such as Ruby and JavaScript.
评论 #25395475 未加载
blackrock超过 4 年前
When do the objects get destroyed or garbage collected?<p>This part was always magical. It seems to free up the local memory usage of the program, but it doesn’t seem to release the memory back to the operating system, until some type of triggered event.
评论 #25393074 未加载
评论 #25396065 未加载
评论 #25391847 未加载
评论 #25392433 未加载
rurban超过 4 年前
This is exactly the reason why python is so slow. The everything is an object approach has no real benefit, as the code is still full of special cases. Faster VMs of course special-case int and float. Best at compile-time.
评论 #25397014 未加载
thrower超过 4 年前
I want to start contributing to open source projects like these, but the process seems somewhat intimidating.<p>Are there any good guides or maybe an introductory tutorial somewhere on how to get involved?
评论 #25396987 未加载
83457超过 4 年前
I did a quick search and didn&#x27;t see it. Does this article cover why python has mutable default arguments?
评论 #25393670 未加载