What the ID () function in Python refers to

Source: Internet
Author: User
This article describes what the ID () function in Python refers to, has a certain reference value, now share to everyone, the need for friends can refer to

The ID () function is used to get the memory address of the object. Many friends don't know what the ID function is in python. The next small series to share this article to help you learn

The explanation given in the official Python document is

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

From this we can see:

1. ID (object) returns the "Social Security Number" of the object, which is unique and unchanging, but the same ID value may occur 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.

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


To determine if two objects are equal, it 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 ' C34/>print S1 is s2  #True  print S1 = = S2  #True  a=2  b=1+1  print A is b  #True  a = 1 9998989890  b = 19998989889 +1  print A is B  #False


The difference between IS and = = is that it is an in-memory comparison, and = = is a comparison of values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.