The id given by python is interpreted as
id(object)
Return the “identity” of an object. This is an integer(or long integer) which is guaranteed to be
unique and
constant forthis 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.
It can be seen from this:
1、 id(object) returns the "ID number" of the object, which is unique and unchanged, but the same id value may appear in the non-overlapping life cycle. The object mentioned here should specifically refer to objects of composite types (such as classes, lists, etc.). For types such as strings and integers, the id of the variable changes with the change of the value.
2、 The id value of an object represents its address in memory in the CPython interpreter. (CPython interpreter: http://zh.wikipedia.org/wiki/CPython)
classObj():
def __init__(self,arg):
self.x=arg
if __name__ =='__main__':
obj=Obj(1)
print id(obj) #32754432
obj.x=2
print id(obj) #32754432
s="abc"
print id(s) #140190448953184
s="bcd"
print id(s) #32809848
x=1
print id(x) #15760488
x=2
print id(x)
In addition, when using is to determine whether two objects are equal, the basis is this id value
classObj():
def __init__(self,arg):
self.x=arg
def __eq__(self,other):return self.x==other.x
if __name__ =='__main__':
obj1=Obj(1)
obj2=Obj(1)
print obj1 is obj2 #False
print obj1 == obj2 #True
lst1=[1]
lst2=[1]
print lst1 is lst2 #False
print lst1 == lst2 #True
s1='abc'
s2='abc'
print s1 is s2 #True
print s1 == s2 #True
a=2
b=1+1
print a is b #True
a =19998989890
b =19998989889+1
print a is b #False
The difference between is and == is that is is a comparison in memory, and == is a value comparison.
Knowledge point expansion:
Python id() function
description
The id() function returns the unique identifier of the object, which is an integer.
The id() function in CPython is used to obtain the memory address of an object.
grammar
id syntax:
id([object])
Parameter Description:
object — The object.
return value
Returns the memory address of the object.
Instance
The following example shows how to use id:
a ='runoob'id(a)4531887632
b =1id(b)140588731085608
So far, this article on what is the id function of python is introduced here. For more information about what is the id function in python, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts