更新时间:2022-05-17 22:16:28
这是许多 Python 用户的绊脚石.对象引用语义与 C 程序员所习惯的不同.
This is a stumbling block for many Python users. The object reference semantics are different from what C programmers are used to.
我们来看第一种情况.当您说 a = b = 0
时,会创建一个新的 int
对象,其值为 0
并创建了对它的两个引用(一个是 a
和另一个是 b
).这两个变量指向同一个对象(我们创建的整数).现在,我们运行 a = 4
.创建一个新的 int
对象,其值为 4
并且 a
指向它.这意味着,对 4
的引用次数为 1,对 0
的引用次数减少了 1.
Let's take the first case. When you say a = b = 0
, a new int
object is created with value 0
and two references to it are created (one is a
and another is b
). These two variables point to the same object (the integer which we created). Now, we run a = 4
. A new int
object of value 4
is created and a
is made to point to that. This means, that the number of references to 4
is one and the number of references to 0
has been reduced by one.
将此与 C 中的 a = 4
进行比较,其中写入了 a
指向"的内存区域.a = b = 4
在 C 中意味着 4
被写入两块内存 - 一块用于 a
,另一块用于 b代码>.
Compare this with a = 4
in C where the area of memory which a
"points" to is written to. a = b = 4
in C means that 4
is written to two pieces of memory - one for a
and another for b
.
现在第二种情况,a = Klass(2)
创建一个类型为 Klass
的对象,将它的引用计数加一,并使 a
> 指向它.b = a
简单地获取 a
指向的内容,使 b
指向同一事物,并将事物的引用计数加一.这与如果您执行 a = b = Klass(2)
时会发生的情况相同.尝试打印 a.num
和 b.num
是相同的,因为您正在取消引用相同的对象并打印属性值.您可以使用 id
内置函数来查看对象是否相同(id(a)
和 id(b)
将返回相同的标识符).现在,您可以通过为对象的属性之一分配值来更改对象.由于 a
和 b
指向同一个对象,因此当通过 a
或b
.事情就是这样.
Now the second case, a = Klass(2)
creates an object of type Klass
, increments its reference count by one and makes a
point to it. b = a
simply takes what a
points to , makes b
point to the same thing and increments the reference count of the thing by one. It's the same as what would happen if you did a = b = Klass(2)
. Trying to print a.num
and b.num
are the same since you're dereferencing the same object and printing an attribute value. You can use the id
builtin function to see that the object is the same (id(a)
and id(b)
will return the same identifier). Now, you change the object by assigning a value to one of it's attributes. Since a
and b
point to the same object, you'd expect the change in value to be visible when the object is accessed via a
or b
. And that's exactly how it is.
现在,为您解答问题.