python tips: is同一性运算符

is用来判断两个对象是不是同一个对象,通过id()函数取对象地址进行判断。python会缓存一些小的整数,以及只包含字母数字以及下划线的字符串。所以在对于这些值的时候,is判断为True。

>>> a = 10
>>> b = 10
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = "123"
>>> b = "123"
>>> a is b
True
>>> a = "urifsdka"
>>> b = "urifsdka"
>>> a is b
True
>>> a = "siru289ur2jfkjhsfa98uasfjh293ruhfkjsdhf9w28u3rhhskfj_fhihfiwehfkjsahfushf"
>>> b = "siru289ur2jfkjhsfa98uasfjh293ruhfkjsdhf9w28u3rhhskfj_fhihfiwehfkjsahfushf"
>>> a is b #对于很长的字符串也是如此
True
>>> a = "a b" #包含空格
>>> b = "a b"
>>> a is b
False
>>> a = "ab " #包含空格
>>> b = "ab "
>>> a is b
False
>>> a = 10.0
>>> b = 10.0
>>> a is b
False
>>> a = "a#b"
>>> b = "a#b"
>>> a is b
False
>>> 
posted @ 2019-06-07 13:20  luoheng  阅读(401)  评论(0编辑  收藏  举报