久久久久久久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 的意思不同)
主站蜘蛛池模板: 国产一区二区三区四区在线观看 | 999国产视频 | yiren22 亚洲综合 | 午夜久久久| 精品免费国产视频 | 国产一级一级毛片 | 欧美综合一区二区三区 | 国产高清视频在线观看 | 中文字幕成人av | 羞羞视频免费在线观看 | 日本三级网址 | 亚洲一卡二卡 | 国内精品视频在线观看 | 中文字幕不卡视频在线观看 | 91久久久久久 | 日韩免费一二三区 | 狠狠操婷婷 | 999久久久 | 久久国产三级 | 狠狠色综合久久丁香婷婷 | 亚洲精品自拍视频 | 国产精品久久久久久久久久三级 | 国产欧美精品一区二区三区 | 国产精品国产精品国产专区不片 | 成人免费看片 | 免费亚洲一区二区 | 久久精品一区 | 一区二区三区视频在线 | 一区二区三区视频在线免费观看 | 国产欧美精品一区二区色综合 | 欧美日韩成人 | 成人av电影在线观看 | 密室大逃脱第六季大神版在线观看 | 天天操天天射天天舔 | 久久久久久久网 | 久草视频观看 | 国产精品日产欧美久久久久 | 久久久久成人精品 | 亚洲人成在线播放 | 亚洲小视频在线播放 | 视频在线一区 |