Python中的id()函数

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

null
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
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享