In Python, what is the id () function, and pythonid function?

Source: Internet
Author: User

In Python, what is the id () function, and pythonid function?

What is the explanation in the Python official documentation?

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.

We can see that:

1. The id (object) returns the "id card number" of the object, which is unique and unchanged. However, the same id value may appear in the lifecycle that does not overlap. The object mentioned here should be a composite type object (such as class and list). For string and integer types, the variable id changes with the value.

2. The id value of an object in the CPython interpreter represents its address in the memory. (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 

In addition, the id value is used to determine whether two objects are equal.

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 

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

Summary

The above is a small Editor to introduce the id function in Python, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.