官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 宾阳县| 安西县| 白玉县| 乐至县| 利津县| 满洲里市| 叙永县| 辉南县| 衡南县| 祥云县| 原平市| 余江县| 太和县| 禹州市| 方城县| 鹤壁市| 安丘市| 井冈山市| 白银市| 和平区| 隆林| 泸水县| 金沙县| 湄潭县| 开鲁县| 宕昌县| 绥宁县| 邯郸市| 蒲城县| 和平县| 江西省| 额尔古纳市| 荣成市| 漠河县| 尉犁县| 耒阳市| 同心县| 新绛县| 广宁县| 太谷县| 项城市|