ID (object)
function: returns the "Social Security Number" of the object, unique and unchanging, but the same ID value may appear during a non-coincident life cycle. The object mentioned here should refer specifically to objects of compound types (such as classes, lists, etc.), and for strings, integers, and so on, the ID of the variable changes with the value.
python version: python2.x python3.x
Python official English document explains :
Return the "Identity" of an object. The is a integer (or long Integer) which is guaranteed to being unique and constant for the this object during its lifetime. The same ID () value is objects with non-overlapping lifetimes.
CPython implementation Detail:this is the address of the object in memory.
Note: The ID value of an object in the CPython interpreter represents its address in memory (the Python C language implementation interpreter).
code example:
Class OBJ (): 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) #15760464
To determine if two objects are equal, this is the ID value
The difference between IS and = = is that it is an in-memory comparison, and = = is a comparison of values