isTrue如果两个变量指向同一个对象(
==如果变量引用的对象相等),则将返回。
>>> 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)