Python function-id function

Source: Internet
Author: User
Python function-id function id (object)

Function: the id card number of the object is returned, which is unique and unchanged. However, the same id value may appear in the non-overlapping lifecycle. 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.

Python version: Python2.x Python3.x

Python official English document explanation:

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.


Note: the id value of an object in the CPython interpreter represents its address in the memory (the interpreter implemented in Python C ).


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

This id is used to determine whether two objects are equal.

The difference between is and = is that is a comparison in memory, while = 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.