码迷,mamicode.com
首页 > 编程语言 > 详细

python is同一性运算符

时间:2018-08-14 18:57:14      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:一个   空格   字符   缓存   运算   字母数   包含   划线   判断   

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同一性运算符

标签:一个   空格   字符   缓存   运算   字母数   包含   划线   判断   

原文地址:https://www.cnblogs.com/luoheng23/p/9476251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!