What is the id function of python

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

What is the id function of python
What is the function body of python
What is the use of Python
What is the scope of python variables
What is the prospect of python development
What is the use of python's pip
What are the required parameters of python
What is an anonymous function in Python
Where is the pip path of python3
What are the advantages of python language
python3 realizes the function of mask drawing
What is the difference between python and pycharm
Consolidate the foundation of Python (4)
What is the difference between synchronous and asynchronous Python?
Consolidate the foundation of Python(7)
Detailed explanation of the principle of Python function parameter classification
Consolidate the foundation of Python(5)
What is introspection in python
What is object-oriented in python
What is Python variable scope
Consolidate the foundation of Python (3)
Minimalism is the soul of Python | Python code to find bugs (10)
The usage of wheel in python
How Python implements the mail function
What is list comprehension in python
Is there function overloading in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
​What are the numbers in Python?
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
What is the difference between a python script and a web page
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
Python implements the sum of fractional sequences
How to run id function in python
Python|Interestingly explain the mystery of ID number
Two days of learning the basics of Python
The essence of Python language: Itertools library
The specific method of python instantiation object
What system is good for self-study python
What is a sequence table in Python
Is there a helper function in python
Where is the directory after python installation
What kind of work can python do
The specific method of python import library
Solve the conflict of multiple versions of python
How to use the round function in python
Python 3.9 is here!
How to use the zip function in Python
Python realizes the development of student management system