id(object)
Function: What is returned is 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.
Python version: Python2.x Python3.x
Python official English documentation explains:
Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and
constant for this 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.
Note: The id value of an object in the CPython interpreter represents its address in memory (the interpreter implemented by Python's c language).
Code example:
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) #15760464
When using is to judge whether two objects are equal, the basis is this id value
The difference between is and == is that is is a comparison in memory, and == is a comparison of values
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 the operation of id functions in python is introduced. For more information about how id functions of python work, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts