4.5.2對象身份比較

                                        整數對象和
字符串對象是不可變對象,所以 Python 會很高效的緩存它們。這會造成我們認爲 Python 應該
創建新對象時,它卻沒有創建新對象的假象。

 

 >>> foo1 = 4.3
>>> type(foo1)
<type 'float'>
>>> type(4.3)
<type 'float'>
>>> fool is 4.3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fool' is not defined
>>> foo1 is 4.3
False
>>> foo2 = 3.0 + 1.3
>>> foo1 is foo2
False
>>> foo2
4.2999999999999998
>>> foo1
4.2999999999999998
>>> foo3 = 4.3
>>> foo3
4.2999999999999998
>>> foo3 is foo1
False
>>> foo4 = foo1
>>> foo4 is foo1
True
>>> int1 = 5
>>> int1
5
>>> int2 = 3 + 2
>>> int2
5
>>> int1 is int2
True
>>>

>>> id(int1)
160829192
>>> id(int2)
160829192
>>> id(foo3)
161455492
>>> id(foo1)
160974508
>>> id(foo4)
160974508
>>>


發佈了45 篇原創文章 · 獲贊 3 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章