CPython - 在内部,堆栈和堆上存储什么?
在 C# 中,值类型(例如:int、float 等)存储在堆栈中。方法参数也可以存储在堆栈中。然而,大多数其他内容都存储在堆上。这包括列表、对象等。
我想知道,CPython 内部是否也做同样的事情?它在栈上存储什么,在堆上又放什么?
In C#, Value Types (eg: int, float, etc) are stored on the stack. Method parameters may also be stored on the stack as well. Most everything else, however, is stored on the heap. This includes Lists, objects, etc.
I was wondering, does CPython do the same thing internally? What does it store on the stack, and what does it put on the heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CPython 实现中的所有 Python 对象都位于堆上。您可以在此处 在文档中:
请注意,Python 本身只是一种语言,并没有说明内存管理等内部机制应该如何工作;这是留给实施者的细节。
All Python objects in the CPython implementation go on the heap. You can read in detail how Python's memory management works here in the documentation:
Note that Python itself is just a language, and says nothing about how internals like memory management should work; this is a detail left to implementers.
Python 的运行时仅处理对对象的引用(它们都位于堆中):Python 堆栈上的内容(作为其字节码操作的操作数和结果)始终是引用(对位于其他位置的值)。
Python's runtime only deals in references to objects (which all live in the heap): what goes on Python's stack (as operands and results of its bytecode operations) are always references (to values that live elsewhere).