📜  Python中的id()函数

📅  最后修改于: 2022-05-13 01:54:37.046000             🧑  作者: Mango

Python中的id()函数

介绍
id() 是Python中的内置函数。
句法:

id(object)

正如我们所见,该函数接受单个参数并用于返回对象的标识。该标识在该对象的生命周期内必须是唯一且恒定的。具有非重叠生命周期的两个对象可能具有相同的 id() 值。如果我们将其与 C 相关联,那么它们实际上是内存地址,在Python中它是唯一的 id。该函数一般在Python内部使用。

例子:

The output is the identity of the 
object passed. This is random but 
when running in the same program, 
it generates unique and same identity. 

Input : id(1025)
Output : 140365829447504
Output varies with different runs

Input : id("geek")
Output : 139793848214784
# This program shows various identities
str1 = "geek"
print(id(str1))
  
str2 = "geek"
print(id(str2))
  
# This will return True
print(id(str1) == id(str2))
  
# Use in Lists
list1 = ["aakash", "priya", "abdul"]
print(id(list1[0]))
print(id(list1[2]))
  
# This returns false
print(id(list1[0])==id(list1[2]))

输出:

140252505691448
140252505691448
True
140252505691840
140252505739928
False