問題描述
我在對模塊進行單元測試時遇到了一個令人困惑的問題.該模塊實際上是在轉換值,我想比較這些值.
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.
這篇關于'is' 運算符對浮點數的行為異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!