📅  最后修改于: 2020-10-30 05:35:23             🧑  作者: Mango
Python id()函数返回对象的标识。这是一个整数,保证是唯一的。此函数将参数作为对象,并返回表示身份的唯一整数。具有不重叠生存期的两个对象可能具有相同的id()值。
id (object)
object:这是要返回其ID的对象。
它返回一个唯一的整数。
让我们来看一些id()函数的示例,以了解其功能。
# Python id() function example
# Calling function
val = id("Javatpoint") # string object
val2 = id(1200) # integer object
val3 = id([25,336,95,236,92,3225]) # List object
# Displaying result
print(val)
print(val2)
print(val3)
输出:
139963782059696
139963805666864
139963781994504
# Python id() function example
class Student:
def __init__(self, id, name):
self.id = id
self.name = name
student = Student(101,"Mohan")
print(student.id)
print(student.name)
# Calling function
val = id(student) # student class object
# Displaying result
print("Object id:",val)
输出:
101
Mohan
Object id: 140157155861392
# Python id() function example
l1 = [1,2,3,4]
l2 = [1,2,3,4]
l3 = [3,5,6,7]
# Calling function
id1 = id(l1)
id2 = id(l2)
id3 = id(l3)
# Displaying result
print((l1==l2),(l1==l3))
# Objects with the same values can have different ids
print((id1==id2),(id1==id3))
# l1 and l2 returns True, while id1 and id2 returns False
输出:
True False
False False