Can someone explain this to me?
>>> [] is [] False >>> () is () True >>> (1,) is (1,) False
I understand that I have to use "==" instead of "is" to compare values, I'm just wondering why this is so?
isbased on the identity of the object. IE, are left and right the same object?
is
In all these cases, the objects are usually different (since you have six separate literals). However, empty tuples are one and the same object due to implementation-dependent internment. As you noted, you should never rely on this behavior.
Note that mutable objects cannot be interned, which means the first must be false.
id. GC'd, !
>>> id([])==id([]) True
>>> id([1,2,3])==id(["A","B","C"]) True
: , , python, :
>>> a = () >>> b = () >>> a is b True
:
>>> a = [] >>> b = [] >>> a.append("foo") >>> print a,b ['foo'] []
, a b , a b.
. Python , , ( / - (1) , , , , - (1,), , ).