如何创建对变量的引用? [英] How to create a reference to a variable?

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

问题描述

在代码中:

y = 7
x = y
x = 8

现在,y 将是 7,x 将是 8.但实际上我想更改 y.我可以分配 y 的引用然后这样做吗?

Now, y will be 7 and x will be 8. But actually I wan to change y. Can I assign the reference of y and do that?

例如,在 C++ 中可以实现相同的事情:

For example, in C++ the same thing can be achieved as:

int y = 8;
int &x = y;
x = 9;

现在 y &x 将是 9

Now both y & x will be 9

推荐答案

不,你不能.正如其他答案指出的那样,您可以(ab?)使用可变对象的别名来实现类似的效果.然而,这与 C++ 引用不同,我想解释一下实际发生的情况以避免任何误解.

No, you cannot. As other answer point out, you can (ab?)use aliasing of mutable objects to achieve a similar effect. However, that's not the same thing as C++ references, and I want to explain what actually happens to avoid any misconceptions.

您会看到,在 C++(和其他语言)中,变量(和对象字段、集合中的条目等)是一个存储位置,您可以写入一个值(例如,整数、对象或指针)指向该位置.在这个模型中,引用是存储位置(任何类型)的别名——当你分配给一个非引用变量时,你将一个值(即使它只是一个指针,它仍然是一个值)复制到存储位置;当您分配给引用时,您将复制到其他地方的存储位置.请注意,您不能更改引用本身 - 一旦它被绑定(并且必须在您创建一个之后立即更改)所有分配给它的不是引用而是改变引用的任何内容.

You see, in C++ (and other languages), a variable (and object fields, and entries in collections, etc.) is a storage location and you write a value (for instance, an integer, an object, or a pointer) to that location. In this model, references are an alias for a storage location (of any kind) - when you assign to a non-reference variable, you copy a value (even if it's just a pointer, it's still a value) to the storage location; when you assign to a reference, you copy to a storage location somewhere else. Note that you cannot change a reference itself - once it is bound (and it has to as soon as you create one) all assignments to it alter not the reference but whatever is referred to.

在 Python(和其他语言)中,变量(和对象字段、集合中的条目等)只是一个名称.值在某处其他地方(例如散布在整个堆上),并且变量引用(不是在 C++ 引用的意义上,更像是一个指针减去指针运算)到一个值.多个名称可以引用同一个值(这通常是一件好事).Python(和其他语言)将引用值所需的任何内容称为引用,尽管与 C++ 引用和传递引用等内容非常无关.分配给变量(或对象字段,或...)只是使其引用另一个值.整个存储位置模型不适用于 Python,程序员从不处理值的存储位置.他存储和打乱的都是 Python 引用,这些不是 Python 中的值,因此它们不能成为其他 Python 引用的目标.

In Python (and other languages), a variable (and object fields, and entries in collections, etc.) is a just a name. Values are somewhere else (e.g. sprinkled all over the heap), and a variable refers (not in the sense of C++ references, more like a pointer minus the pointer arithmetic) to a value. Multiple names can refer to the same value (which is generally a good thing). Python (and other languages) calls whatever is needed to refer to a value a reference, despite being pretty unrelated to things like C++ references and pass-by-reference. Assigning to a variable (or object field, or ...) simply makes it refer to another value. The whole model of storage locations does not apply to Python, the programmer never handles storage locations for values. All he stores and shuffles around are Python references, and those are not values in Python, so they cannot be target of other Python references.

所有这些都与值的可变性无关——例如,对于整数和列表来说是一样的.您不能采用引用任何一个的变量,并覆盖它指向的对象.您只能告诉对象修改其自身的一部分 - 例如,更改它包含的某些引用.

All of this is independent of mutability of the value - it's the same for ints and lists, for instance. You cannot take a variable that refers to either, and overwrite the object it points to. You can only tell the object to modify parts of itself - say, change some reference it contains.

这是一个更具限制性的模型吗?也许吧,但大多数时候它已经足够强大了.如果不是,您可以使用如下所示的自定义类或(等效但不太明显的)单元素集合来解决它.

Is this a more restrictive model? Perhaps, but it's powerful enough most of the time. And when it isn't you can work around it, either with a custom class like the one given below, or (equivalent, but less obvious) a single-element collection.

class Reference:
    def __init__(self, val):
        self._value = val # just refers to val, no copy

    def get(self):
        return self._value

    def set(self, val):
        self._value = val

这仍然不允许您为常规"变量或对象字段取别名,但您可以有多个变量引用同一个 Reference 对象(可变单例集合替代方案同上)).您只需要小心始终使用 .get()/.set()(或 [0]).

That still won't allow you to alias a "regular" variable or object field, but you can have multiple variables referring to the same Reference object (ditto for the mutable-singleton-collection alternative). You just have to be careful to always use .get()/.set() (or [0]).

这篇关于如何创建对变量的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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