is 和 == 的区别

is 和 == 操作符的区别

python官方解释:

== 的meaning为equal;

is的meaning为object identity;

is 判断对象是否相等,即身份是否相同,使用id值判断;
== 判断对象的值是否相等。
 
id值是什么?
id()函数官网解释:
id(object)
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
CPython implementation detail: This is the address of the object in memory.
 
返回对象的身份,即在对象生命周期中对象保证唯一不变的整数。不重叠的两个对象可能会有同样的id值。
对于CPython解释器:id值是对象在内存中的地址。
 
 
Cpython是什么?
python解释器用来执行.py文件。从python官网下载并安装好Python后,我们就直接获得了一个官方版本的解释器:CPython。这个解释器是用C语言开发的,所以叫CPython。在命令行下运行python就是启动CPython解释器。CPython是使用最广的Python解释器。


 

原文地址:https://www.cnblogs.com/testfan/p/8082447.html