Python中的id()函数指的什么呢?
发布网友
发布时间:2023-05-04 14:26
我来回答
共1个回答
热心网友
时间:2023-10-30 15:41
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 ring 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解释器:)
?
1
234
5678910111213141516
class
Obj():
def
__init__(self
,arg):
self
.x
=
arg
if
__name__
=
=
__main__
obj
=
Obj(1)
print
id
(obj)
#32754432
obj.x