Two variables in Python have the same id
:
Python中的两个变量具有相同的id:
a = 10
b = 10
a is b
>>> True
If I take two list
s:
如果我拿两个列表:
a = [1, 2, 3]
b = [1, 2, 3]
a is b
>>> False
according to this link Senderle answered that immutable object references have the same id and mutable objects like lists have different ids.
根据这个链接,Senderle回答说,不可变对象引用具有相同的id,而像列表这样的可变对象具有不同的id。
So now according to his answer, tuples should have the same ids - meaning:
所以现在根据他的回答,元组应该有相同的ID - 意思是:
a = (1, 2, 3)
b = (1, 2, 3)
a is b
>>> False
Ideally, as tuples are not mutable, it should return True
, but it is returning False
!
理想情况下,由于元组不可变,它应该返回True,但它返回False!
What is the explanation?
解释是什么?
4 个解决方案
#1
46
Immutable objects don't have the same id
, and as a mater of fact this is not true for any type of objects that you define separately. Every time you define an object in Python, you'll create a new object with a new identity.
不可变对象没有相同的id,事实上,对于您单独定义的任何类型的对象都不是这样。每次在Python中定义对象时,都将创建一个具有新标识的新对象。
But there are some exceptions for small integers (between -5 and 256) and small strings (interned strings, with a special length (usually less than 20 character)) which are singletons and have same id
(actually one object with multiple pointer). You can check this fact like following:
但是对于小整数(介于-5和256之间)和小字符串(实际字符串,具有特殊长度(通常小于20个字符)),有一些例外,它们是单例并具有相同的id(实际上是一个具有多个指针的对象)。您可以检查以下事实:
>>> 30 is 20 + 10
True
>>>
>>> 300 is 200 + 100
False
>>> 'aa' * 2 is 'a' * 4
True
>>> 'aa' * 20 is 'a' * 40
False
And for a custom object:
对于自定义对象:
>>> class A:
... pass
...
>>> A() is A() # Every time you create an instance you'll have a new instance with new identity
False
Also note that the is
operator will check the object's identity, not the value. If you want to check the value you should use ==
:
另请注意,is运算符将检查对象的标识,而不是值。如果你想检查你应该使用的值==:
>>> 300 == 3*100
True
And since there is no such rule for tuples (other types) if you define the two same tuples in any size they'll get their own ids:
并且因为对于元组(其他类型)没有这样的规则,如果你定义任何大小的两个相同的元组,它们将得到它们自己的id:
>>> a = (1,)
>>> b = (1,)
>>>
>>> a is b
False
And note that the fact of singleton integers and interned strings is true even when you define them within mutable and immutable objects:
请注意,即使在可变和不可变对象中定义它们,单例整数和实习字符串的事实也是如此:
>>> a = (100, 700, 400)
>>>
>>> b = (100, 700, 400)
>>>
>>> a[0] is b[0]
True
>>> a[1] is b[1]
False
#2
19
Immutable !=
same object.*
不变的!=同一个对象。*
An immutable object is simply an object whose state cannot be altered; and that is all. When a new object is created, a new address will be assigned to it. As such, checking if the addresses are equal with is
will return False
.
不可变对象只是一个状态不能改变的对象;这就是全部。创建新对象时,将为其分配新地址。因此,检查地址是否相等将返回False。
The fact that 1 is 1
or "a" is "a"
returns True
is due to integer caching and string interning performed by Python so do not let it confuse you; it is not related with the objects in question being mutable/immutable.
1为1或“a”为“a”的事实返回True是由于Python执行的整数缓存和字符串实习,所以不要让它混淆你;它与有问题的对象无关是可变的/不可变的。
*Empty immutable objects do refer to the same object and their is
ness does return true, this is a special implementation specific case, though.
*空的不可变对象确实引用同一个对象,它们的isness确实返回true,但这是一个特殊的实现特定情况。
#3
15
Take a look at this code:
看看这段代码:
>>> a = (1, 2, 3)
>>> b = (1, 2, 3)
>>> c = a
>>> id(a)
178153080L
>>> id(b)
178098040L
>>> id(c)
178153080L
In order to figure out why a is c
is evaluated as True
whereas a is b
yields False
I strongly recommend you to run step-by-step the snippet above in the Online Python Tutor. The graphical representation of the objects in memory will provide you with a deeper insight into this issue (I'm attaching a screenshot).
为了弄清楚为什么a被c评估为True而a是b得到False我强烈建议你在Online Python Tutor中逐步运行上面的代码片段。内存中对象的图形表示将为您提供对此问题的更深入了解(我附上了截图)。
#4
0
Check below code.. tupils a and b are retaining their older references(ID) back when we have assigned their older values back. (BUT, THIS WILL NOT BE THE CASE WITH LISTS AS THEY ARE MUTABLE)
检查下面的代码..当我们分配旧的值时,学生a和b保留他们的旧引用(ID)。 (但是,由于它们是可以修复的,所以不会是这样的情况)
Initially a and b have same values ( (1,2) ), but they have difference IDs. After alteration to their values, when we reassign value (1,2) to a and b, they are now referencing to THEIR OWN same IDs (88264264 and 88283400 respectively).
最初a和b具有相同的值((1,2)),但它们具有不同的ID。在更改其值后,当我们将值(1,2)重新分配给a和b时,它们现在引用自己相同的ID(分别为88264264和88283400)。
>>> a = (1,2)
>>> b = (1,2)
>>> a , b
((1, 2), (1, 2))
>>> id(a)
88264264
>>> id(b)
88283400
>>> a = (3,4)
>>> b = (3,4)
>>> id(a)
88280008
>>> id(b)
88264328
>>> a = (1,2)
>>> b = (1,2)
>>> id(a)
88264264
>>> id(b)
88283400
>>> a , b
((1, 2), (1, 2))
>>> id(a) , id(b)
(88264264, 88283400)
>>>
**Check the link Why don't tuples get the same ID when assigned the same values? also after reading this. Another case also been discussed here.
**检查链接为什么在分配相同的值时,元组是否获得相同的ID?读完之后也看了。这里也讨论了另一个案例。
#1
46
Immutable objects don't have the same id
, and as a mater of fact this is not true for any type of objects that you define separately. Every time you define an object in Python, you'll create a new object with a new identity.
不可变对象没有相同的id,事实上,对于您单独定义的任何类型的对象都不是这样。每次在Python中定义对象时,都将创建一个具有新标识的新对象。
But there are some exceptions for small integers (between -5 and 256) and small strings (interned strings, with a special length (usually less than 20 character)) which are singletons and have same id
(actually one object with multiple pointer). You can check this fact like following:
但是对于小整数(介于-5和256之间)和小字符串(实际字符串,具有特殊长度(通常小于20个字符)),有一些例外,它们是单例并具有相同的id(实际上是一个具有多个指针的对象)。您可以检查以下事实:
>>> 30 is 20 + 10
True
>>>
>>> 300 is 200 + 100
False
>>> 'aa' * 2 is 'a' * 4
True
>>> 'aa' * 20 is 'a' * 40
False
And for a custom object:
对于自定义对象:
>>> class A:
... pass
...
>>> A() is A() # Every time you create an instance you'll have a new instance with new identity
False
Also note that the is
operator will check the object's identity, not the value. If you want to check the value you should use ==
:
另请注意,is运算符将检查对象的标识,而不是值。如果你想检查你应该使用的值==:
>>> 300 == 3*100
True
And since there is no such rule for tuples (other types) if you define the two same tuples in any size they'll get their own ids:
并且因为对于元组(其他类型)没有这样的规则,如果你定义任何大小的两个相同的元组,它们将得到它们自己的id:
>>> a = (1,)
>>> b = (1,)
>>>
>>> a is b
False
And note that the fact of singleton integers and interned strings is true even when you define them within mutable and immutable objects:
请注意,即使在可变和不可变对象中定义它们,单例整数和实习字符串的事实也是如此:
>>> a = (100, 700, 400)
>>>
>>> b = (100, 700, 400)
>>>
>>> a[0] is b[0]
True
>>> a[1] is b[1]
False
#2
19
Immutable !=
same object.*
不变的!=同一个对象。*
An immutable object is simply an object whose state cannot be altered; and that is all. When a new object is created, a new address will be assigned to it. As such, checking if the addresses are equal with is
will return False
.
不可变对象只是一个状态不能改变的对象;这就是全部。创建新对象时,将为其分配新地址。因此,检查地址是否相等将返回False。
The fact that 1 is 1
or "a" is "a"
returns True
is due to integer caching and string interning performed by Python so do not let it confuse you; it is not related with the objects in question being mutable/immutable.
1为1或“a”为“a”的事实返回True是由于Python执行的整数缓存和字符串实习,所以不要让它混淆你;它与有问题的对象无关是可变的/不可变的。
*Empty immutable objects do refer to the same object and their is
ness does return true, this is a special implementation specific case, though.
*空的不可变对象确实引用同一个对象,它们的isness确实返回true,但这是一个特殊的实现特定情况。
#3
15
Take a look at this code:
看看这段代码:
>>> a = (1, 2, 3)
>>> b = (1, 2, 3)
>>> c = a
>>> id(a)
178153080L
>>> id(b)
178098040L
>>> id(c)
178153080L
In order to figure out why a is c
is evaluated as True
whereas a is b
yields False
I strongly recommend you to run step-by-step the snippet above in the Online Python Tutor. The graphical representation of the objects in memory will provide you with a deeper insight into this issue (I'm attaching a screenshot).
为了弄清楚为什么a被c评估为True而a是b得到False我强烈建议你在Online Python Tutor中逐步运行上面的代码片段。内存中对象的图形表示将为您提供对此问题的更深入了解(我附上了截图)。
#4
0
Check below code.. tupils a and b are retaining their older references(ID) back when we have assigned their older values back. (BUT, THIS WILL NOT BE THE CASE WITH LISTS AS THEY ARE MUTABLE)
检查下面的代码..当我们分配旧的值时,学生a和b保留他们的旧引用(ID)。 (但是,由于它们是可以修复的,所以不会是这样的情况)
Initially a and b have same values ( (1,2) ), but they have difference IDs. After alteration to their values, when we reassign value (1,2) to a and b, they are now referencing to THEIR OWN same IDs (88264264 and 88283400 respectively).
最初a和b具有相同的值((1,2)),但它们具有不同的ID。在更改其值后,当我们将值(1,2)重新分配给a和b时,它们现在引用自己相同的ID(分别为88264264和88283400)。
>>> a = (1,2)
>>> b = (1,2)
>>> a , b
((1, 2), (1, 2))
>>> id(a)
88264264
>>> id(b)
88283400
>>> a = (3,4)
>>> b = (3,4)
>>> id(a)
88280008
>>> id(b)
88264328
>>> a = (1,2)
>>> b = (1,2)
>>> id(a)
88264264
>>> id(b)
88283400
>>> a , b
((1, 2), (1, 2))
>>> id(a) , id(b)
(88264264, 88283400)
>>>
**Check the link Why don't tuples get the same ID when assigned the same values? also after reading this. Another case also been discussed here.
**检查链接为什么在分配相同的值时,元组是否获得相同的ID?读完之后也看了。这里也讨论了另一个案例。