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

时间:2023-02-14 18:13:34

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

6 个解决方案

#1


82  

Thats what is is for: x is y

x是y

#2


10  

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

y = x为真,y = z为假。

#3


4  

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

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

In [1]: x1, x2 = 'foo', 'foo'
In [2]: x1 == x2
Out[2]: True
In [3]: x1 is x2
Out[3]: True
In [4]: id(x1), id(x2)
Out[4]: (4367888016, 4367888016)
In [5]: x2 = x2 + 'bar'
In [6]: x1 is x2
Out[6]: False
In [7]: id(x1), id(x2)
Out[7]: (4367888016, 4369529616)

#4


4  

While the two correct solutions x is z and id(x) == id(z) 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

当两个正确的解决方案x是z和id(x) == id(z)时,我想指出python的实现细节。Python将整数存储为对象,作为一种优化,它在开始时(-5到256)生成一堆小整数,并将每个拥有一个小值的整数的变量指向这些预先初始化的对象。更多信息

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.

这意味着整数对象初始化到相同的小数字(5 - 256)检查两个物体是相同的是否会返回true(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

#5


0  

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.

我真的很喜欢有一个视觉反馈,这就是为什么我有时会打开http://www.pythontutor.com/visualize.html#mode=edit看看内存是如何分配的,以及什么在引用什么。

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

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

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

#6


0  

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."

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

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.

很显然,每次您更改值时,对象都是由身份更改所指示的重新创建的。x=3行后,x=3.14行没有错误,并给出x的不同标识、类型和值。

#1


82  

Thats what is is for: x is y

x是y

#2


10  

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

y = x为真,y = z为假。

#3


4  

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

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

In [1]: x1, x2 = 'foo', 'foo'
In [2]: x1 == x2
Out[2]: True
In [3]: x1 is x2
Out[3]: True
In [4]: id(x1), id(x2)
Out[4]: (4367888016, 4367888016)
In [5]: x2 = x2 + 'bar'
In [6]: x1 is x2
Out[6]: False
In [7]: id(x1), id(x2)
Out[7]: (4367888016, 4369529616)

#4


4  

While the two correct solutions x is z and id(x) == id(z) 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

当两个正确的解决方案x是z和id(x) == id(z)时,我想指出python的实现细节。Python将整数存储为对象,作为一种优化,它在开始时(-5到256)生成一堆小整数,并将每个拥有一个小值的整数的变量指向这些预先初始化的对象。更多信息

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.

这意味着整数对象初始化到相同的小数字(5 - 256)检查两个物体是相同的是否会返回true(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

#5


0  

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.

我真的很喜欢有一个视觉反馈,这就是为什么我有时会打开http://www.pythontutor.com/visualize.html#mode=edit看看内存是如何分配的,以及什么在引用什么。

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

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

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

#6


0  

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."

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

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.

很显然,每次您更改值时,对象都是由身份更改所指示的重新创建的。x=3行后,x=3.14行没有错误,并给出x的不同标识、类型和值。