久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

'is' 運算符對浮點數的行為異常

#39;is#39; operator behaves unexpectedly with floats(is 運算符對浮點數的行為異常)
本文介紹了'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.

這篇關于'is' 運算符對浮點數的行為異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 黄色片www | 久草视| 欧美成人性生活视频 | 五月婷婷影院 | 日韩免费精品视频 | 欧美成人精品一区二区三区 | 亚洲午夜18毛片在线看 | 嫩草嫩草嫩草嫩草 | 青青草视频在线观看 | 亚洲精品911| 四虎影院最新地址 | 亚洲欧美在线视频 | 日韩欧美国产高清91 | 国产女优在线 | 日皮视频在线观看 | 亚洲九九九 | 一级片日韩 | 欧美一区二区三 | 国产精品911 | 日韩色在线 | 欧美精品在线观看视频 | 国产精品久久久久久无人区 | 91精品国产成人www | 日韩av网站在线观看 | 亚洲精品一二三四 | 在线观看日韩欧美 | 日本精品国产 | 96在线视频 | 性欧美8khd高清极品 | 黄色一级在线观看 | 午夜精品久久久久久久 | 丰满岳乱妇一区二区 | 成人理论影院 | 精品影院 | 亚洲激情欧美 | 欧美黄色片网站 | 成人毛片100免费观看 | 高清乱码男女免费观看 | 日韩网站免费观看 | 午夜伦理视频 | 黄色国产在线观看 |