The ID function of the Python function

Source: Internet
Author: User
ID (object)

function: returns the "Social Security Number" of the object, unique and unchanging, but the same ID value may appear 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.

python version: python2.x python3.x

Python official English document explains :

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.

Note: The ID value of an object in the CPython interpreter represents its address in memory (the Python C language implementation interpreter).

code example:

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, this is the ID value

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.