What does the id() function in Python refer to?

不言
Release: 2023-03-24 06:56:02
Original
6056 people have browsed it

The content of this article is about what the id() function in Python refers to. It has a certain reference value. Now I share it with you. Friends in need can refer to it

id() function Used to get the memory address of the object. Many friends don’t know what the id function in python is? Next, the editor will share with you this article to help you learn

The explanation given by the official Python documentation is

id(object)
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.

It can be seen from this:

1. id(object) returns the "ID card number" of the object , unique and unchanged, but the same id value may appear in non-overlapping life cycles. The objects mentioned here should specifically refer to composite type objects (such as classes, lists, etc.). For types such as strings and integers, the id of the variable changes as the value changes.

2. The id value of an object represents its address in memory in the CPython interpreter. (CPython interpreter: http://zh.wikipedia.org/wiki/CPython)


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
Copy after login


For other orders, use is to judge When two objects are equal, the basis is the id value


class Obj(): 
 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
Copy after login


The difference between is and == is that is is in memory Comparison, and == is a comparison of values

Related recommendations:

Python radiation code implementation

The above is the detailed content of What does the id() function in Python refer to?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!