Python中的id()函数_怪异现象

2023-01-31 01:01:48 python 函数 id

python中有一个内置函数叫id(obj) ,可以获取obj在Python中的实际内存,比如:

>>> id(13)
8791398340000
>>> id(int)
8791397851616

那么,id(obj)和id(obj)是不是相等的呢?让我们来测试一下(python3.7)

>>> id(13)
8791398340000
>>> id(13)
8791398340000
>>> id(int)
8791397851616
>>> id(int)
8791397851616
原来他们相等啊。。。不对!!id(257)和id(257)就不相等!
>>> id(257)
52174288
>>> id(257)
52174768
但是又有......
>>> id(257)==id(257)
True

这是为什么呢?大家可以参考一下GitHub上名叫What's The F*ck of python的文档,答案就在里面。

相关文章