python is同一性运算符

Posted luoheng23

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 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
>>>  

以上是关于python is同一性运算符的主要内容,如果未能解决你的问题,请参考以下文章

基础篇Python中is和==的区别

python的is和is not运算符是干啥用的

Python中is和==的区别

Python中is和==的区别

Python入门-2编程基本概念:12同一运算符-整数缓存问题

is is not == !=之间的区别