python function id function

巴扎黑
Release: 2017-08-17 10:46:46
Original
2047 people have browsed it

id(object)

Function: Returns the "ID card number" of the object, which is unique and unchanged, but the same may appear in non-overlapping life cycles id value. 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.

Python version: Python2.x Python3.x

Python English official 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 represents its address in memory in the CPython interpreter (the interpreter implemented in Python's c language).

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

When using is to determine whether two objects are equal, the basis is the id value

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

The above is the detailed content of python function id function. 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!