python技巧 is 和 ==

丁壯發表於2018-11-14
  • is 判斷變數是否指向同一個物件
  • == 判斷變數引用的物件是否相等
In [2]: a=[1,2]

In [3]: b=a

In [4]: a == b
Out[4]: True

In [5]: a is b
Out[5]: True

In [6]: c = list(a)

In [7]: a == c
Out[7]: True

In [8]: a is c
Out[8]: False

In [9]: b is c
Out[9]: False

 

相關文章