python is 输出

a='Hi'
b='Hi'
str1='Ta shi yi ge chi huo'
str2='Ta shi yi ge chi huo'
print(a is b) #true
print(a==b) #true
print(str1 is str2) #false
print(str1==str2) #true

a=256
b=256
print(a is b) #true
print(a==b) #true
a=257
b=257
print(a is b) #false
print(a==b) #true
is 是判断id是否相等即id(a)==id(b) 但为什么只是数值不同 字符串不同 就会让is结果不一样?
这与python缓冲池有关,就int 而言,如果它在区间[-5,256]之间
a=-5
b=-5

不会为b开辟新的空间,只是b指向a的引用
对于字符串也会有对应的判断条件,当一个字符串所有字符都在一个预设字符串中时,就也不开辟新空间 只是引用
这与java中的==很像

猜你喜欢

转载自blog.csdn.net/xlm11/article/details/78020979