在 Python 中传递值
当你将像列表、数组这样的集合传递给Python中的另一个函数时,它会复制它,还是只是一个指针?
When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Python 按值传递对象引用。
Python passes references-to-objects by value.
问题是,整个引用/值概念不适合 python。 Python 没有变量的“值”。 Python 只有对象和引用对象的名称。
因此,当您调用函数并在括号内放入“名称”时,如下所示:
传递
myname
指向的实际对象,而不是 namemyname
本身。 在函数内部,给出了另一个名称 (x
) 来引用传递的同一对象。如果函数内部的对象是可变的,您可以修改该对象,但您无法更改外部名称所指向的内容。 与您这样做时发生的情况相同
因此我可以回答您的问题:
它是“按值传递”,但所有值都只是对对象的引用。
Thing is, the whole reference/value concept won't fit into python. Python has no "value" of a variable. Python has only objects and names that refer to objects.
So when you call a function and put a "name" inside the parenthesis, like this:
The actual object that
myname
is pointing is passed, not the namemyname
itself. Inside the function another name (x
) is given to refer to the same object passed.You can modify the object inside the function if it is mutable, but you can't change what the outside name is pointing to. Just the same that happens when you do
Therefore I can answer your question with:
it is "pass by value" but all values are just references to objects.
这里的答案很有帮助,但我发现有必要展示这种我没有看到的细微区别,我已经通过后续的 CL 实验向自己证明了这一点:
“num”不会在这里进行更改,因为它是一个不可变的 Number 对象 [支持我的观点 1.]:
list[0]
这里也是一个不可变的 Number 对象。那么,作为一个不可变的 Number 对象,
list[0]
是如何改变的(支持我的观点 2),而上面示例的 Number 对象“num”却没有改变? Number 对象list[0]
包含在可变列表对象“list”中,而第一个示例中的“num”只是一个非包含的 Number 对象( >不可变)。虽然出发点是好的,但我觉得 @Stephen Pape 评价最高的答案(在下面引用),以及其他一些类似的答案,并不完全正确(这促使我写下这个答案):
我上面的第二个代码实验显示了一个 Number 对象('list[0]')在方法内被更改,然后函数外部的原始实例发生了更改。
Answers here have been helpful, but I find the need to exhibit this fine distinction which I haven't seen covered, which I've proven to myself with the subsequent CL experiment:
'num' does not change here because it is an immutable Number object [supports my point 1.]:
list[0]
here is an immutable Number object also.So how did
list[0]
, being an immutable Number object, change (supports my point 2.) while the above example's Number object 'num' did not? The immutable Number objectlist[0]
is contained within the mutable list object 'list', while 'num' from the 1st example is just a non-contained Number object (immutable).Although well-intended, I feel @Stephen Pape top-rated answer (quoted below), and some other similar ones, were not totally correct (and that motivated me to write this answer):
My 2nd code experiment above shows a Number object ('list[0]') being altered within a method, and then the original instance outside the function changed.
传递引用,但如果参数是不可变对象,则在方法内修改它将创建一个新实例。
A reference is passed, but if the parameter is an immutable object, modifying it within the method will create a new instance.
对象已传递。 不是副本,而是对底层对象的引用。
The object is passed. Not a copy, but a reference to the underlying object.
引用:
By reference:
我还建议查看
copy
模块:Python 复制文档< /a>
它将帮助您了解根本问题以及如何使用它来执行您自己的深层复制。
I would also recommend looking at the
copy
module:Python documentation for copy
It will help you to understand the underlying issues and how to use it to perform your own deep copy.
请让我举一个简单的例子
,它会产生以下结果
Please let me give a humble example
which produces the following result