Python中id函数的作用是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计583个文字,预计阅读时间需要3分钟。
Python官方对id()函数的解释为:返回对象的身份。这是一个在对象的生命周期内保证唯一且恒定的整数(或长整数)。两个具有非重叠生命周期的对象具有不同的id值。
python官方给出的id解释为
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解释器里就代表它在内存中的地址。
本文共计583个文字,预计阅读时间需要3分钟。
Python官方对id()函数的解释为:返回对象的身份。这是一个在对象的生命周期内保证唯一且恒定的整数(或长整数)。两个具有非重叠生命周期的对象具有不同的id值。
python官方给出的id解释为
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解释器里就代表它在内存中的地址。

