可能重複:
Python 「is」 operator behaves unexpectedly with integers爲什麼(0-6)是-6 =假?
今天我試圖調試我的項目,我會得到這個分析的幾個小時後:
>>> (0-6) is -6
False
但是,
>>> (0-5) is -5
True
你能向我解釋一下,爲什麼? 也許這是某種錯誤或非常奇怪的行爲。
> Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2
>>> type(0-6)
<type 'int'>
>>> type(-6)
<type 'int'>
>>> type((0-6) is -6)
<type 'bool'>
>>>
這就是徹頭徹尾的古怪 – Wug 2012-07-13 18:30:46
什麼導致你首先使用'is'?除了'is/is not None'外,這不是Python中經常使用的東西。 – 2012-07-13 18:35:03
@拉塞爾的評論觸及頭部 - 問題在於有人顯然使用「是」來比較數字,並期望它像'='一樣運作,這是一個不正確的期望。 – LarsH 2012-07-13 20:11:34