This question already has an answer here:
这个问题在这里已有答案:
- Understanding Python's “is” operator 9 answers
理解Python的“是”运算符9答案
What is the difference between these two pieces of code in the boolean world:
布尔世界中这两段代码之间有什么区别:
1 is 1
and
1 == 1
I found two web pages that describe it, but I can't see the difference since I don't know how to get different results:
我找到了两个描述它的网页,但由于我不知道如何得到不同的结果,我看不出差异:
http://www.informit.com/articles/article.aspx?p=459269&seqNum=10
https://docs.python.org/2/library/stdtypes.html
On the 2nd page, I found the operators. On the 1st page, which I looked at second, it described a difference that doesn't tell me when I'd do this and get different results. That's my big question, is when does doing one differ results from the other?
在第2页,我找到了运营商。在第一页,我看了第二页,它描述了一个差异,并没有告诉我何时这样做并得到不同的结果。这是我最大的问题,那就是什么时候做一个不同的结果呢?
Obviously, there are cases where one will be true and the other false, right?
显然,有些情况会是真的而另一种是假的,对吧?
4 个解决方案
#1
1
When using variables, it can cause incorrect results:
使用变量时,可能会导致错误的结果:
>>> foo = [1, 2, 3]
>>> foo is [1, 2, 3]
False
>>> foo == [1, 2, 3]
True
Look at the documentation, here
请看这里的文档
==
means "equal", while is
is an object identity.
==表示“相等”,而is是对象标识。
#2
1
Equality is well-defined, type by type, depending only on the values being compared. Identity depends on whether a certain value is duplicated, or stored only once and referred to repeatedly.
平等是明确定义的,按类型排序,仅取决于所比较的值。身份取决于某个值是否重复,或仅存储一次并重复引用。
For immutable values, identity's therefore an implementation detail!
对于不可变值,身份因此是一个实现细节!
Consider, for example:
例如,考虑一下:
2>>> x = 23
2>>> y = 23
2>>> x is y
True
2>>> x = 232323
2>>> y = 232323
2>>> x is y
False
Small values like 23
happen to be "cached" (an implementation detail!) in CPython in the hope of saving memory if they're used often; large values like 232323
are not so cached -- again, 100% an implementation detail. You'd never want to depend on such behavior!
像CP这样的小值恰好在CPython中被“缓存”(一个实现细节!),以期在经常使用时节省内存;像232323这样的大值不是那么缓存 - 再次,100%的实现细节。你永远不想依赖这种行为!
#3
0
In addition to these, you can change the behavior of ==
for classes that you define.
除此之外,您还可以更改您定义的类的==行为。
class A:
def __init__(self, a, b):
self.a = a
self.b = b
def __eq__(self, other):
#let us ignore b in our comparison!
return self.a == other.a
a1 = A(1,2)
a2 = A(1,3)
print(a1 == a2) #prints true
print(a1 is a2) #prints false
The behavior of is
cannot be changed and will always compare the memory addresses where the objects themselves are stored.
is的行为无法更改,并且将始终比较存储对象本身的内存地址。
#4
0
a is b checks if a points to the same memory location as b a == b checks if the values of a and b are the same. e.g.
a是b检查指向与b a == b相同的内存位置是否检查a和b的值是否相同。例如
a=10 (computer stores '10' at 0xf34852)
b=10 (computer stores '10' at 0xfeac54)
a==b (true)
a is b (false)
#1
1
When using variables, it can cause incorrect results:
使用变量时,可能会导致错误的结果:
>>> foo = [1, 2, 3]
>>> foo is [1, 2, 3]
False
>>> foo == [1, 2, 3]
True
Look at the documentation, here
请看这里的文档
==
means "equal", while is
is an object identity.
==表示“相等”,而is是对象标识。
#2
1
Equality is well-defined, type by type, depending only on the values being compared. Identity depends on whether a certain value is duplicated, or stored only once and referred to repeatedly.
平等是明确定义的,按类型排序,仅取决于所比较的值。身份取决于某个值是否重复,或仅存储一次并重复引用。
For immutable values, identity's therefore an implementation detail!
对于不可变值,身份因此是一个实现细节!
Consider, for example:
例如,考虑一下:
2>>> x = 23
2>>> y = 23
2>>> x is y
True
2>>> x = 232323
2>>> y = 232323
2>>> x is y
False
Small values like 23
happen to be "cached" (an implementation detail!) in CPython in the hope of saving memory if they're used often; large values like 232323
are not so cached -- again, 100% an implementation detail. You'd never want to depend on such behavior!
像CP这样的小值恰好在CPython中被“缓存”(一个实现细节!),以期在经常使用时节省内存;像232323这样的大值不是那么缓存 - 再次,100%的实现细节。你永远不想依赖这种行为!
#3
0
In addition to these, you can change the behavior of ==
for classes that you define.
除此之外,您还可以更改您定义的类的==行为。
class A:
def __init__(self, a, b):
self.a = a
self.b = b
def __eq__(self, other):
#let us ignore b in our comparison!
return self.a == other.a
a1 = A(1,2)
a2 = A(1,3)
print(a1 == a2) #prints true
print(a1 is a2) #prints false
The behavior of is
cannot be changed and will always compare the memory addresses where the objects themselves are stored.
is的行为无法更改,并且将始终比较存储对象本身的内存地址。
#4
0
a is b checks if a points to the same memory location as b a == b checks if the values of a and b are the same. e.g.
a是b检查指向与b a == b相同的内存位置是否检查a和b的值是否相同。例如
a=10 (computer stores '10' at 0xf34852)
b=10 (computer stores '10' at 0xfeac54)
a==b (true)
a is b (false)