python的id()函数介绍
2016-09-15 07:53
281 查看
本文为大家介绍python中的id()函数,有需要的朋友不妨参考下
>>> a = 2.5
>>> b = 2.5
>>> c = b
>>> a is c
False
>>> a = 2
>>> b = 2
>>> c = b
>>> a is c
True
在使用is函数的时候去打印a,b分别被赋值为2.5 和2的情况,发现:
>>> a = 2
>>> b = 2
>>> id(a)
21132060
>>> id(b)
21132060
>>> a = 2.5
>>> b = 2.5
>>> id(a)
19622112
>>> id(b)
29321464
当a,b为2的时候id相同,而为2.5的时候不同,这种情况在string字符串的时候也会出现,即当很短的a,b赋值很短的字符串的时候,它们的id值相同,而很长的则不会;
查阅了如下的文章:
http://stackoverflow.com/questions/4293408/ids-of-immutable-types
http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change
之后,得到一个简单的结论:解释器在对值很小的int和很短的字符串的时候做了一点小优化,只分配了一个对象,让它们id一样了。
相关文章推荐
- python的id()函数介绍
- python的id()函数介绍
- python中RawConfigParser类、ConfigParser类、SafeConfigParser类中函数介绍
- python 装饰器功能以及函数参数使用介绍
- python的id()函数的一个小方面(转载)
- python的lambda函数介绍
- Python中的id函数到底是什么?
- Python 一些函数介绍
- python的lambda函数介绍
- 【Python】 strip(),split()函数介绍
- python strip()函数 介绍
- python strip()函数 介绍
- Dive Into Python 学习记录3-getattr 介绍/过滤列表/and or/lambda 函数
- python strip()函数 介绍
- python strip()函数 介绍
- python 装饰器功能以及函数参数使用介绍
- python的id()函数解密过程
- python的id函数
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
- 【python】filter,map,reduce和lambda函数介绍