Python中的id()函数指的什么


Posted in Python onOctober 17, 2017

Python官方文档给出的解释是

id(object)
Return the “identity” of an object. This is an integer (or long 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.

由此可以看出:

1、id(object)返回的是对象的“身份证号”,唯一且不变,但在不重合的生命周期里,可能会出现相同的id值。此处所说的对象应该特指复合类型的对象(如类、list等),对于字符串、整数等类型,变量的id是随值的改变而改变的。

2、一个对象的id值在CPython解释器里就代表它在内存中的地址。(CPython解释器:http://zh.wikipedia.org/wiki/CPython)

class Obj(): 
 def __init__(self,arg): 
  self.x=arg 
if __name__ == '__main__': 
 obj=Obj(1) 
 print id(obj)  #32754432 
 obj.x=2 
 print id(obj)  #32754432 
 s="abc" 
 print id(s)   #140190448953184 
 s="bcd" 
 print id(s)   #32809848 
 x=1 
 print id(x)   #15760488 
 x=2 
 print id(x)   #15760464

令外,用is判断两个对象是否相等时,依据就是这个id值

class Obj(): 
 def __init__(self,arg): 
  self.x=arg 
 def __eq__(self,other): 
  return self.x==other.x 
if __name__ == '__main__': 
 obj1=Obj(1) 
 obj2=Obj(1) 
 print obj1 is obj2 #False 
 print obj1 == obj2 #True 
 lst1=[1] 
 lst2=[1] 
 print lst1 is lst2 #False 
 print lst1 == lst2 #True 
 s1='abc' 
 s2='abc' 
 print s1 is s2  #True 
 print s1 == s2  #True 
 a=2 
 b=1+1 
 print a is b  #True 
 a = 19998989890 
 b = 19998989889 +1 
 print a is b  #False

is与==的区别就是,is是内存中的比较,而==是值的比较

总结

以上所述是小编给大家介绍Python中的id函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python实现的金山快盘的签到程序
Jan 17 Python
Django1.7+python 2.78+pycharm配置mysql数据库教程
Nov 18 Python
python编写的最短路径算法
Mar 25 Python
Python中函数的参数传递与可变长参数介绍
Jun 30 Python
Python 2.x如何设置命令执行的超时时间实例
Oct 19 Python
Python实现二维数组输出为图片
Apr 03 Python
如何使用VSCode愉快的写Python于调试配置步骤
Apr 06 Python
python使用pymongo操作mongo的完整步骤
Apr 13 Python
python分割一个文本为多个文本的方法
Jul 22 Python
Python 基于FIR实现Hilbert滤波器求信号包络详解
Feb 26 Python
PyCharm License Activation激活码失效问题的解决方法(图文详解)
Mar 12 Python
查看已安装tensorflow版本的方法示例
Apr 19 Python
Python中int()函数的用法浅析
Oct 17 #Python
一文总结学习Python的14张思维导图
Oct 17 #Python
python 中的int()函数怎么用
Oct 17 #Python
python遍历序列enumerate函数浅析
Oct 17 #Python
浅谈python中的正则表达式(re模块)
Oct 17 #Python
深入理解Django的自定义过滤器
Oct 17 #Python
Python引用类型和值类型的区别与使用解析
Oct 17 #Python
You might like
十大感人催泪爱情动漫 第一名至今不忍在看第二遍
2020/03/04 日漫
PHP中防止SQL注入实现代码
2011/02/19 PHP
Apache连接PHP后无法启动问题解决思路
2015/06/18 PHP
浅析php中array_map和array_walk的使用对比
2016/11/20 PHP
php四种定界符详解
2017/02/16 PHP
PHP中非常有用却鲜有人知的函数集锦
2019/08/17 PHP
用js实现预览待上传的本地图片
2007/03/15 Javascript
js实现对ajax请求面向对象的封装
2016/01/08 Javascript
关于JS中match() 和 exec() 返回值和属性的测试
2016/03/21 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
浅谈js中function的参数默认值
2017/02/20 Javascript
Node.js 中exports 和 module.exports 的区别
2017/03/14 Javascript
基于vue-cli vue-router搭建底部导航栏移动前端项目
2018/02/28 Javascript
vue组件中iview的modal组件爬坑问题之modal的显示与否应该是使用v-show
2019/04/12 Javascript
jquery实现选项卡切换代码实例
2019/05/14 jQuery
kafka调试中遇到Connection to node -1 could not be established. Broker may not be available.
2019/09/17 Javascript
jQuery实现提交表单时不提交隐藏div中input的方法
2019/10/08 jQuery
python中django框架通过正则搜索页面上email地址的方法
2015/03/21 Python
python多线程方式执行多个bat代码
2016/06/07 Python
python实现下载整个ftp目录的方法
2017/01/17 Python
Python 类的继承实例详解
2017/03/25 Python
python处理按钮消息的实例详解
2017/07/11 Python
详解Python核心编程中的浅拷贝与深拷贝
2018/01/07 Python
python如何爬取个性签名
2018/06/19 Python
Python简单读写Xls格式文档的方法示例
2018/08/17 Python
Python3 修改默认环境的方法
2019/02/16 Python
python多线程分块读取文件
2019/08/29 Python
Python调用OpenCV实现图像平滑代码实例
2020/06/19 Python
详解HTML5中表单验证的8种方法介绍
2016/12/19 HTML / CSS
香港时装购物网站:ZALORA香港
2017/04/23 全球购物
班级入场式解说词
2014/02/01 职场文书
期末自我鉴定
2014/02/02 职场文书
财务科科长岗位职责
2014/03/10 职场文书
班级年度安全计划书
2014/05/01 职场文书
PyCharm 安装与使用配置教程(windows,mac通用)
2021/05/12 Python
海弦WR-800F
2022/04/05 无线电