如果两个变量指向同一个对象,则返回
True;如果变量引用的对象相等,则返回
=。
>>> a = [1, 2, 3]>>> b = a>>> b is a True>>> b == aTrue# Make a new copy of list `a` via the slice operator, # and assign it to variable `b`>>> b = a[:] >>> b is aFalse>>> b == aTrue
在你的例子中,第二个测试只起作用,因为Python缓存小整数对象,这是一个实现细节。对于较大的整数,这不起作用:
>>> 1000 is 10**3False>>> 1000 == 10**3True
字符串文本也是如此:
>>> "a" is "a"True>>> "aa" is "a" * 2True>>> x = "a">>> "aa" is x * 2False>>> "aa" is intern(x*2)True



