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? - 路 ...
随机推荐
- Win10 企业版 激活 批处理
cd %SystemRoot%\System32 wscript.exe slmgr.vbs /upk wscript.exe slmgr.vbs /ipk NPPR9-FWDCX-D2C8J-H87 ...
- SQL注入(字符型)
靶场:sqli-labs @SQLi最重要的一点:别上来就对着输入框注入,完整语句写出来,始终在语句中写完整的,最后把完整的一部分截取出来作为输入 @URL编码:为避免歧义,URL中,如 %2b ...
- gx_dlms 的杂乱记录
DLMS_ERROR_CODE_FALSE W3Jehpnc543MuwUz6ZWDshy5kwbbE9Cw CGXDLMSClient::GetData(CGXByteBuffer& rep ...
- poj2406(求字符串的周期,kmp算法next数组的应用)
题目链接:https://vjudge.net/problem/POJ-2406 题意:求出给定字符串的周期,和poj1961类似. 思路:直接利用next数组的定义即可,当没有周期时,周期即为1. ...
- [转帖]phoronix-test-suite 简介
<工作杂记>之phoronix-test-suite 2017年10月30日 14:32:52 打雷下雨 阅读数 2078更多 分类专栏: # linux 版权声明:本文为博主原创文章 ...
- ES-实战
一.环境准备 操作系统:mac 依赖的软件:JDK1.8.Postman.NodeJs6.0以上.Maven.Idea ES下载:Elastic官方网站: http://www.elastic.co ...
- c# base64及MD5工具类
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- java源码--ArrayList
1.1.ArrayList概述 1)ArrayList是可以动态增长和缩减的索引序列,它是基于数组实现的List类. 2)该类封装了一个动态再分配的Object[]数组,每一个类对象都有一个capac ...
- Python基础 第三章 使用字符串(3)字符串方法&本章小结
字符串的方法非常之多,重点学习一些最有用的,完整的字符串方法参见<Python基础教程(第三版)>附录B. 模块string,虽然风头已小,但其包含了一些字符串方法中没有的常量和函数,故将 ...
- S02_CH10_ User GPIO实验
S02_CH10_ User GPIO实验 在之前的第四章课程中,我们详细的讲解了如何在VIVADO软件下封装一个简单的流水灯程序.在ZYNQ开发过程中,有时候我们可能会需要与ARM硬核进行通信,在这 ...