'is' 运算符对浮点数的行为异常

问题描述

我在对模块进行单元测试时遇到了一个令人困惑的问题.该模块实际上是在转换值,我想比较这些值.

I came across a confusing problem when unit testing a module. The module is actually casting values and I want to compare this values.

==is 相比存在差异(部分,我要注意差异)

There is a difference in comparison with == and is (partly, I'm beware of the difference)

>>> 0.0 is 0.0
True   # as expected
>>> float(0.0) is 0.0
True   # as expected

到目前为止,正如预期的那样,但这是我的问题":

As expected till now, but here is my "problem":

>>> float(0) is 0.0
False
>>> float(0) is float(0)
False

为什么?至少最后一个让我很困惑.float(0)float(0.0) 的内部表示应该相等.与 == 的比较按预期进行.

Why? At least the last one is really confusing to me. The internal representation of float(0) and float(0.0) should be equal. Comparison with == is working as expected.


解决方案

这与 is 的工作方式有关.它检查引用而不是值.如果任一参数分配给同一对象,则返回 True.

This has to do with how is works. It checks for references instead of value. It returns True if either argument is assigned to the same object.

在这种情况下,它们是不同的实例;float(0)float(0) 具有相同的值 ==,但就 Python 而言是不同的实体.CPython 实现还将整数缓存为此范围内的单例对象 -> [x |x ∈ ℤ ∧ -5 ≤ x ≤ 256 ]:

In this case, they are different instances; float(0) and float(0) have the same value ==, but are distinct entities as far as Python is concerned. CPython implementation also caches integers as singleton objects in this range -> [x | x ∈ ℤ ∧ -5 ≤ x ≤ 256 ]:

>>> 0.0 is 0.0
True
>>> float(0) is float(0)  # Not the same reference, unique instances.
False

在这个例子中我们可以演示整数缓存原理:

In this example we can demonstrate the integer caching principle:

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False

现在,如果将浮点数传递给 float(),则简单地返回浮点字面量(short-circuited),就像使用相同的引用一样,因为有无需从现有浮点数实例化新浮点数:

Now, if floats are passed to float(), the float literal is simply returned (short-circuited), as in the same reference is used, as there's no need to instantiate a new float from an existing float:

>>> 0.0 is 0.0
True
>>> float(0.0) is float(0.0)
True

这也可以通过使用 int() 进一步演示:

This can be demonstrated further by using int() also:

>>> int(256.0) is int(256.0)  # Same reference, cached.
True
>>> int(257.0) is int(257.0)  # Different references are returned, not cached.
False
>>> 257 is 257  # Same reference.
True
>>> 257.0 is 257.0  # Same reference. As @Martijn Pieters pointed out.
True

但是,is 的结果也取决于它正在执行的范围(超出此问题/解释的范围),请参考用户:@Jim 对 代码对象.甚至 python 的文档也包含有关此行​​为的部分:

However, the results of is are also dependant on the scope it is being executed in (beyond the span of this question/explanation), please refer to user: @Jim's fantastic explanation on code objects. Even python's doc includes a section on this behavior:

  • 5.9 比较

[7]由于自动垃圾收集、空闲列表和描述符的动态特性,您可能会注意到 is 运算符的某些使用中看似不寻常的行为,例如涉及实例方法或常量之间比较的那些.查看他们的文档以获取更多信息.

[7] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.

相关文章