Python的引用传递 [英] Python's Passing by References

查看:92
本文介绍了Python的引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我试图了解Python的引用传递是如何工作的. 我有一个例子:

Hello I am trying to understand how Python's pass by reference works. I have an example:

>>>a = 1
>>>b = 1
>>>id(a);id(b)
140522779858088
140522779858088

这很合理,因为a和b都引用了与它们具有相同标识的相同值.我不太明白的是这个例子:

This makes perfect sense since a and b are both referencing the same value that they would have the identity. What I dont quite understand is how this example:

>>>a = 4.4
>>>b = 1.0+3.4
>>>id(a);id(b)
140522778796184
140522778796136

与本示例不同:

>>>a = 2
>>>b = 2 + 0
>>>id(a);id(b)
140522779858064
140522779858064

是因为在第三个示例中,解释器将0 int对象视为无"并且没有被识别为需要与变量"a"所引用的对象不同的标识(2)吗?而在第二个示例中,"b"正在添加两个不同的int对象,而解释器正在为要添加的两个对象分配内存,从而为变量"a"提供了与变量"b"不同的标识?

Is it because in the 3rd example the 0 int object is being viewed as "None" by the interpreter and is not being recognized as needing a different identity from the object which variable "a" is referencing(2)? Whereas in the 2nd example "b" is adding two different int objects and the interpreter is allocating memory for both of those objects to be added, which gives variable "a", a different identity from variable "b"?

推荐答案

在您的第一个示例中,由于 interning ,名称ab都引用"了同一对象.赋值语句产生的整数具有相同的id,这仅是因为该赋值语句重用了一个已经存在于内存中的先前存在的对象.这不是整数的可靠行为:

In your first example, the names a and b are both "referencing" the same object because of interning. The assignment statement resulted in an integer with the same id only because it has reused a preexisting object that happened to be hanging around in memory already. That's not a reliable behavior of integers:

>>> a = 257
>>> b = 257
>>> id(a), id(b)
(30610608, 30610728)

如上所述,如果选择足够大的整数,则其行为将与第二个示例中的浮点数相同.而且无论如何,在Python语言中可选都是小整数,这恰好是CPython实现的细节:这是一种性能优化,旨在避免创建新对象的开销.我们可以通过缓存常用的整数实例来加快处理速度,但代价是Python解释器的内存占用量更大.

As demonstrated above, if you pick a big enough integer then it will behave as the floats in your second example behaved. And interning small integers is optional in the Python language anyway, this happens to be a CPython implementation detail: it's a performance optimization intended to avoid the overhead of creating a new object. We can speed things up by caching commonly used integer instances, at the cost of a higher memory footprint of the Python interpreter.

在处理Python时,不要考虑引用"和值",适用于C的模型在这里并不能很好地工作.而是考虑名称"和对象".

Don't think about "reference" and "value" when dealing with Python, the model that works for C doesn't really work well here. Instead think of "names" and "objects".

上图说明了您的第三个示例. 2是对象,ab是名称.我们可以使用不同的名称指向同一对象.对象可以不存在任何名称而存在.

The diagram above illustrates your third example. 2 is an object, a and b are names. We can have different names pointing at the same object. And objects can exist without any name.

分配变量,仅附加名称标签.并且仅删除变量删除名称标签.如果您牢记这个主意,那么Python对象模型将再也不会令您感到惊讶.

Assigning a variable only attaches a nametag. And deleting a variable only removes a nametag. If you keep this idea in mind, then the Python object model will never surprise you again.

这篇关于Python的引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆