久久久久久久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 的意思不同)
主站蜘蛛池模板: 成人影院网站ww555久久精品 | 日本精品一区二区三区视频 | www午夜视频 | 久久国产欧美日韩精品 | 综合在线视频 | 亚洲激情综合 | 亚州精品天堂中文字幕 | 在线观看欧美一区 | 色婷婷亚洲一区二区三区 | 日韩精品在线观看视频 | 久久成人一区 | 黄色永久免费 | 精品中文视频 | 亚州一区二区三区 | 国产欧美精品一区二区三区 | 久久99蜜桃综合影院免费观看 | 天天操天天射天天舔 | 奇米视频777 | 日韩精品成人 | 精品国产一二三区 | 在线看免费 | 日本一区二区三区精品视频 | 色天天综合 | 国产精品视频在线观看 | 一级毛片高清 | 亚洲 自拍 另类 欧美 丝袜 | 伊人久久大香线 | 日韩中文字幕视频在线观看 | 精品在线免费观看视频 | 中文字幕在线一区 | 91精品国产综合久久久久 | 欧美综合久久 | 国产一区中文字幕 | 日韩a在线 | 综合久久av | 国产精品国产a级 | 免费观看毛片 | 在线区| 在线不卡视频 | 在线观看www高清视频 | jdav视频在线观看免费 |