python函数如何返回对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22896094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does python function return objects?
提问by overexchange
In the below diagram, I have a query on name temp
that is returned from function f
在下图中,我对temp
从函数返回的名称进行了查询f
In local frame f
, temp
is a reference variable pointing to an object 6
of class int
, when you say return temp
in f
a reference variable output
which is part of checkPassingMech
frame will point to the same object 6
that temp was pointing to.
在本地框架中f
, temp
是一个指向6
类对象的引用变量int
,当您return temp
在f
作为 框架output
一部分的引用变量中说时,checkPassingMech
将指向6
temp 指向的同一个对象。
My question:
我的问题:
Q1) Is my understanding correct?
Q1) 我的理解正确吗?
Q2) If Q1 is yes, then this diagram is giving an illusion that temp
is not reference type and showing its value in a box rather than with an arrow pointing to 6, am i correct?
Q2) 如果 Q1 是肯定的,那么这个图给人一种错觉,temp
它不是引用类型,而是在一个框中显示它的值,而不是用指向 6 的箭头,我对吗?
Q3) If Q2 is yes, then, Can i say that 6
is actually stored in heap and temp
and output
would be pointing to this heap space from frame(local stack)
Q3)如果 Q2 是肯定的,那么,我可以说它6
实际上存储在堆中,temp
并且output
会从帧(本地堆栈)指向这个堆空间
采纳答案by laike9m
>>> def f():
temp = 6
print(id(temp))
return temp
>>> output = f()
507107408
>>> id(output)
507107408
from doc:
来自文档:
CPython implementation detail: For CPython, id(x) is the memory address where x is stored.
CPython 实现细节:对于 CPython,id(x) 是存储 x 的内存地址。
So strictly speaking, if the value is 6, then output
and temp
is actually pointing to the same object, which is an int
object that is cached when python started.
所以严格来说,如果值为6,那么output
andtemp
实际上是指向同一个对象,也就是int
python启动时缓存的对象。
You may refer to identifying objects, why does the returned value from id(...) change?for more information.
您可能会参考识别对象,为什么 id(...) 的返回值会发生变化?想要查询更多的信息。
回答by ajknzhol
Yes, your understanding is correct, besides Python's runtime only deals in referencesto 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).
是的,您的理解是正确的,除了 Python 的运行时仅处理对对象的引用(它们都位于堆中):Python 堆栈上的内容(作为其字节码操作的操作数和结果)始终是引用(对位于其他地方的值) .
All Python objects in the CPython implementation go on the heap. You can read in detail how Python's memory management works herein the documentation:
CPython 实现中的所有 Python 对象都在堆上。您可以详细Python的内存管理是如何工作的阅读这里的文件中:
Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.
Python 中的内存管理涉及一个包含所有 Python 对象和数据结构的私有堆。这个私有堆的管理是由 Python 内存管理器在内部确保的。Python 内存管理器具有处理各种动态存储管理方面的不同组件,例如共享、分段、预分配或缓存。
回答by flakes
"Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)"
“类型几乎影响对象行为的所有方面。甚至对象标识的重要性在某种意义上也受到影响:对于不可变类型,计算新值的操作实际上可能返回对任何具有相同类型和值的现有对象的引用,而对于可变类型这是不允许的对象。例如,在 a = 1; b = 1 之后,a 和 b 可能会或可能不会引用值为 1 的同一对象,具体取决于实现,但在 c = []; d = [] 之后, c 和 d 保证引用两个不同的、唯一的、新创建的空列表。(注意 c = d = [] 将相同的对象分配给 c 和 d。)”
https://docs.python.org/2/reference/datamodel.html
https://docs.python.org/2/reference/datamodel.html
So you wouldn't be able to just put an arrow pointing at 6, because it is not guaranteed to be the same reference to 6 each time.
所以你不能只放一个指向 6 的箭头,因为它不能保证每次都是 6 的相同引用。
回答by elishaolade
1) Yes. 2) Yes. That is true, temp is not a reference type because it is not referring to any other variables. 3) Yes, the value six is stored in the heap(in this scenario) and temp would be pointing at six no matter the input and the output equals temp which makes it equal to 6.
1) 是的。2) 是的。确实如此,temp 不是引用类型,因为它不引用任何其他变量。3)是的,值 6 存储在堆中(在这种情况下),无论输入和输出是否等于 temp,temp 都将指向 6,这使其等于 6。
You can get a good definition on reference types in this link: http://msdn.microsoft.com/en-us/library/490f96s2.aspx
您可以在此链接中获得关于引用类型的良好定义:http: //msdn.microsoft.com/en-us/library/490f96s2.aspx