一、==是比较两个变量的值不比较内存地址
二、is 是比较内容还有内存地址
注意
Python存在缓存池 对于[-5,255]的整数 会缓存
a = 5 b = 5 a==b T a is b T
a = 1000 b = 1000 a==b T a is b F( 在Python自带的解释器中)
a = 1000 b = 1000 a==b T a is b T( 在Pycharm解释器中优化)
字符串存在缓存池
s = "aa" s1 = "aa" a==b T a is b T
s = "a a" s1 = "a a" a==b T a is b F(有空格不开起intern机制)
类
class Student:
pass
s1 = Student()
s2 = Student()
s1 == s2 F(会调用equals方法默认比较 hashcode) s1 is s2 F(地址就不对,内容同上)
缓存池的实现(待补充)
网友评论