对象和基本类型的分配 [英] Assignment of objects and fundamental types

查看:68
本文介绍了对象和基本类型的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这个代码:

# assignment behaviour for integer
a = b = 0
print a, b # prints 0 0
a = 4
print a, b # prints 4 0 - different!

# assignment behaviour for class object
class Klasa:
    def __init__(self, num):
        self.num = num

a = Klasa(2)
b = a
print a.num, b.num # prints 2 2
a.num = 3
print a.num, b.num # prints 3 3 - the same!

问题:

  1. 为什么赋值运算符对于基本类型和类对象(对于基本类型,它按值复制,对于类对象,它按引用复制)?
  2. 如何仅按值复制类对象?
  3. 如何引用 C++ 中的基本类型 int&b = a?

推荐答案

这是许多 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.numb.num 是相同的,因为您正在取消引用相同的对象并打印属性值.您可以使用 id 内置函数来查看对象是否相同(id(a)id(b) 将返回相同的标识符).现在,您可以通过为对象的属性之一分配值来更改对象.由于 ab 指向同一个对象,因此当通过 ab.事情就是这样.

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.

现在,为您解答问题.

  1. 赋值运算符对这两者的工作方式没有区别.它所做的只是添加对 RValue 的引用并使 LValue 指向它.它总是通过引用"(尽管这个术语在参数传递的上下文中比简单的赋值更有意义).
  2. 如果您想要副本对象,请使用复制模块.
  3. 正如我在第 1 点中所说的,当您完成一项作业时,您总是会转移参考文献.除非您要求,否则永远不会复制.
  1. The assignment operator doesn't work differently for these two. All it does is add a reference to the RValue and makes the LValue point to it. It's always "by reference" (although this term makes more sense in the context of parameter passing than simple assignments).
  2. If you want copies of objects, use the copy module.
  3. As I said in point 1, when you do an assignment, you always shift references. Copying is never done unless you ask for it.

这篇关于对象和基本类型的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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