
上QQ阅读APP看书,第一时间看更新
Equality of value (equivalence) versus equality of identity
Let's contrast that behavior with a test for value-equality, or equivalence. We'll create two identical lists:
>>> p = [4, 7, 11]
>>> q = [4, 7, 11]
>>> p == q
True
>>> p is q
False
Here we see that p and q refer to different objects, but that the objects they refer to have the same value:

Figure 4.10: 'p' and 'q' different list objects with identical values
As you would expect when testing for value-equality, an object should always be equivalent to itself:
>>> p == p
True
Value-equality and identity are fundamentally different notions of "equality", and it's important to keep them separate in your mind.
It's also worth noting that value comparison is something that is defined programatically. When you define types, you can control how that class determines value-equality. In contrast, identity comparison is defined by the language and you can't change that behavior.