本文實例講述了Python中的is和id用法。分享給大家供大家參考。具體分析如下:
(ob1 is ob2) 等價于 (id(ob1) == id(ob2))
首先id函數可以獲得對象的內存地址,如果兩個對象的內存地址是一樣的,那么這兩個對象肯定是一個對象。和is是等價的。Python源代碼為證。
但是請看下邊代碼的這種情況怎么會出現呢?
兩個對象用is判斷是False,用id判斷卻是True,這與我們已知的事實不符啊,這種現象該如何解釋呢?遇到這種情況最好的解決方法就是調用dis模塊去看下兩個比較語句到底做了什么。
下面內容由郵件Leo Jay大牛提供,他解釋的更加通透。
用id(expression a) == id(expression b)來判斷兩個表達式的結果是不是同一個對象的想法是有問題的。
foo.bar 這種形式叫 attribute reference [1],它是表達式的一種。foo是一個instance object,bar是一個方法,這個時候表達式foo.bar返回的結果叫method object。根據文檔:
When an instance attribute is referenced that isn't a data attribute,
its class is searched. If the name denotes a valid class attribute
that is a function object, a method object is created by packing
(pointers to) the instance object and the function object just found
together in an abstract object: this is the method object.
foo.bar本身并不是簡單的名字,而是表達式的計算結果,是一個 method object,在id(foo.bar)這樣的表達式里,method object只是一個臨時的中間變量而已,對臨時的中間變量做id是沒有意義的。
一個更明顯的例子是,
輸出的結果也是True
看 id 的文檔:
Return the “identity” of an object. This is an integer (or long
integer) which is guaranteed to be unique and constant for this object
during its lifetime. Two objects with non-overlapping lifetimes may
have the same id() value.
CPython implementation detail: This is the address of the object in memory.
只有你能保證對象不會被銷毀的前提下,你才能用 id 來比較兩個對象。所以,如果你非要比的話,得這樣寫:
即把兩個表達式的結果綁定到名字上,再來比是不是同一個對象,你才能得到正確的結果。
is表達式也是一樣的,你現在得到了正確的結果,完全是因為 CPython 現在的實現細節決定的。現在的is的實現,是左右兩邊的對象都計算出來,然后再比較這兩個對象的地址是否一樣。萬一哪天改成了,先算左邊,保存地址,把左邊釋放掉,再算右邊,再比較的話,你的is的結果可能就錯了。官方文檔里也提到了這個問題 。我認為正確的方法也是像id那樣,先把左右兩邊都計算下來,并顯式綁定到各自的名字上,然后再用is判斷。
希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選