Skip to content Skip to sidebar Skip to footer

Comparison Of Collections Containing Non-reflexive Elements

In python, a value x is not always constrained to equal itself. Perhaps the best known example is NaN: >>> x = float('NaN') >>> x == x False Now consider a list

Solution 1:

Per the docs,

In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element x, x == x is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison, and may be surprising: The non-reflexive not-a-number values for example result in the following comparison behavior when used in a list:

 >>> nan = float('NaN')
 >>> nan is nan
 True
 >>> nan == nan
 False                 <-- the defined non-reflexive behavior of NaN
 >>> [nan] == [nan]
 True                  <-- list enforces reflexivity and tests identity first

Post a Comment for "Comparison Of Collections Containing Non-reflexive Elements"