Python 中 is 與 == 的區別

Python 中 == 是判斷值是否相等,is 是判斷同一性,換句話說,x is y 至關於 id(x) == id(y) python

x = y = (1, 2, 3)
z = (1, 2, 3)

print id(x)
print id(y)
print id(z)

if x == z:
    print 'x == z'
else:
    print 'x != z'

if x is z:
    print 'x is z'
else:
    print 'x is not z'
輸出結果:

>>>21708312
>>>21708312
>>>22195096
>>>x == z
>>>x is not z
相關文章
相關標籤/搜索