问题:比较两个变量是否在python中引用了同一对象

如何检查两个变量是否引用同一对象?

x = ['a', 'b', 'c']
y = x                 # x and y reference the same object
z = ['a', 'b', 'c']   # x and z reference different objects

How to check whether two variables reference the same object?

x = ['a', 'b', 'c']
y = x                 # x and y reference the same object
z = ['a', 'b', 'c']   # x and z reference different objects

回答 0

就是这样x is y返回Trueif xy是否为同一对象。

That’s what is for: x is y returns True if x and y are the same object.


回答 1

y is xTruey is zFalse

y is x will be True, y is z will be False.


回答 2

虽然两个正确的解决方案x is z,并id(x) == id(z)已经公布,我想指出的Python的实现细节。Python将整数存储为对象,作为一种优化,Python会在其开始处生成一堆小整数(-5到256),并将每个具有小整数的变量指向每个预初始化的对象。更多信息

这意味着对于初始化为相同小数(-5到256)的整数对象,检查两个对象是否相同将返回true(就我所知这是一个实现细节,在ON C-Pyhon上),而对于较大的对象数字,仅当一个对象从另一个对象初始化时,它才返回true。

> i = 13
> j = 13
> i is j
True

> a = 280
> b = 280
> a is b
False

> a = b
> a
280
> a is b
True

While the two correct solutions x is z and id(x) == id(z) have already been posted, I want to point out an implementation detail of python. Python stores integers as objects, as an optimization it generates a bunch of small integers at its start (-5 to 256) and points EVERY variable holding an integer with a small value to these preinitialized objects. More Info

This means that for integer objects initialized to the same small numbers (-5 to 256) checking if two objects are the same will return true (ON C-Pyhon, as far as I am aware this is an implementation detail), while for larger numbers this only returns true if one object is initialized form the other.

> i = 13
> j = 13
> i is j
True

> a = 280
> b = 280
> a is b
False

> a = b
> a
280
> a is b
True

回答 3

您还可以使用id()来检查每个变量名称所引用的唯一对象。

In [1]: x1, x2 = 'foo', 'foo'

In [2]: x1 == x2
Out[2]: True

In [3]: id(x1), id(x2)
Out[3]: (4509849040, 4509849040)

In [4]: x2 = 'foobar'[0:3]

In [5]: x2
Out[5]: 'foo'

In [6]: x1 == x2
Out[6]: True

In [7]: x1 is x2
Out[7]: False

In [8]: id(x1), id(x2)
Out[8]: (4509849040, 4526514944)

You can also use id() to check which unique object each variable name refers to.

In [1]: x1, x2 = 'foo', 'foo'

In [2]: x1 == x2
Out[2]: True

In [3]: id(x1), id(x2)
Out[3]: (4509849040, 4509849040)

In [4]: x2 = 'foobar'[0:3]

In [5]: x2
Out[5]: 'foo'

In [6]: x1 == x2
Out[6]: True

In [7]: x1 is x2
Out[7]: False

In [8]: id(x1), id(x2)
Out[8]: (4509849040, 4526514944)

回答 4

我真的很想收到视觉反馈,这就是为什么有时我只打开http://www.pythontutor.com/visualize.html#mode=edit来查看内存分配方式以及引用什么的原因。

在此处输入图片说明

添加了这个很棒的gif,因为此回复是关于可视化的。

I really like to have a visual feedback, that’s why I sometimes just open up http://www.pythontutor.com/visualize.html#mode=edit to see how the memory is allocated and what is referencing what.

enter image description here

Added this awesome gif as this reply is about visualizing..


回答 5

这来自docs.python.org:“每个对象都有一个标识,一个类型和一个值。创建后,对象的标识就永远不会改变;您可以将其视为对象在内存中的地址。“ is”运算符比较两个对象的标识; id()函数返回一个表示其标识的整数。”

显然,每次更改值时,都会根据标识更改指示重新创建对象。x = 3行,后跟x = 3.14行,没有错误,并且为x提供了不同的标识,类型和值。

This is from docs.python.org: “Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.”

Apparently every time you change the value the object is recreated as indicated by the identity changing. The line x=3 followed by the line x=3.14 gives no error & gives different identities, types and values for x.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。