Is there a difference between `==` and `is` in Python?
is
will return True
if two variables point to the same object, ==
if the objects referred to by the variables are equal.
>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True
In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:
>>> 1000 is 10**3
False
>>> 1000 == 10**3
True
The same holds true for string literals:
>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True
Is there a difference between `==` and `is` in Python?的更多相关文章
- Is there a difference between `==` and `is` in Python?
There is a simple rule of thumb to tell you when to use == or is. == is for value equality. Use it w ...
- 【LeetCode】389. Find the Difference 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...
- python运维开发坎坷之路-01
前言 2014年9月,新疆乌鲁木齐,在51CTO学院看着alex老师的python教学视频,不得不说这是我第一次接触python这门高级语言,从最开始的一无所知到现在能够用python写脚本,再到未来 ...
- Do we need other languages other than C and C++?
There were hundreds of or thousands of programming languages created since the invention of computer ...
- RColorBrewer的使用
RColorBrewer是一个R包,使用http://colorbrewer2.org/这个网站提供的颜色.我们画一个包括八个box的boxplot时,或者在x-y散点图上画六条线时,该怎样选择颜色呢 ...
- Django Model field reference
===================== Model field reference ===================== .. module:: django.db.models.field ...
- python的面试问题
WHAT 1. 什么是Python? Python是一种编程语言,它有对象.模块.线程.异常处理和自动内存管理.可以加入与其他语言的对比.下面是回答这一问题的几个关键点: a. Python是一种解释 ...
- swagger 自动生成接口测试用例
---整体更新一波--- 1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的: 2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加 ...
- Python面试题整理-更新中
几个链接: 编程零基础应当如何开始学习 Python ? - 路人甲的回答 网易云课堂上有哪些值得推荐的 Python 教程? - 路人甲的回答 怎么用最短时间高效而踏实地学习 Python? - 路 ...
随机推荐
- [MSCOCO] Ubuntu16.04下使用 tylin/coco-caption 评价 MSCOCO Caption(配置,及Demo运行)
Github链接:https://github.com/tylin/coco-caption Ubuntu版本信息 Linux内核版本号:Linux version 4.15.0-51-generic ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- PJzhang:目录扫描复合工具dirmap
猫宁!!! 参考:https://www.freebuf.com/sectool/200890.html github地址: https://github.com/H4ckForJob/dirmap ...
- PJzhang:docker基础知识的2个疗程-one
猫宁!!! 参考:http://virtual.51cto.com/art/201805/572135.htm https://www.cnblogs.com/rkit/p/9237696.html ...
- 如何通过Exchange2010 OWA更改过期密码
很多Exchange 2003管理员都通过IISADMPWD虚拟目录为员工提供用户密码修改功能,这大大方便了移动用户和非加入域用户在密码到期时的更改操作.您也许已经注意到:Windows Server ...
- Reactor系列(七)flatMap映射
#java##reactor##flatMap# 视频讲解: https://www.bilibili.com/video/av79582009/ FluxMonoTestCase.java pack ...
- java实现List<People>的排序
1.首先新建测试的实体类(People类): import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsCon ...
- 【转贴】NUMA的取舍与优化设置
NUMA的取舍与优化设置 https://www.cnblogs.com/tcicy/p/10191505.html 在os层numa关闭时,打开bios层的numa会影响性能,QPS会下降15-30 ...
- mybatis 的一对一关联查询association
现在项目的列表查询数据需要查一个总数count, 如果直接写在同一个sql里面,会导致查询速度很慢, 因此,想到使用关联查询,例子如下: 附上代码: 其中遇到的坑哟: 1.association中的s ...
- win7 配置 用户/系统DSN (解决plsql odbc导入器 DSN 没有选项)
(2013-05-07 15:07:29) 转载▼ 标签: it 老笔记本越来越卡,最近重新做了win7的系统 从sql server中导出的 excel文件 准备通过plsql倒入的oracle ...