How to run id function in python

id(object)

Function: What is returned is the "ID number" of the object, which is unique and unchanged, but the same id value may appear in the non-overlapping life cycle. The object mentioned here should specifically refer to objects of composite types (such as classes, lists, etc.). For types such as strings and integers, the id of the variable changes with the change of the value.

Python version: Python2.x Python3.x

Python official English documentation explains:

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 memory (the interpreter implemented by Python's c language).

Code example:

classObj(): 
 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

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

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

Knowledge point expansion:

Python id() function

description

The id() function returns the unique identifier of the object, which is an integer.

The id() function in CPython is used to obtain the memory address of an object.

grammar

id syntax:

id([object])

Parameter Description:

object — The object.

return value

Returns the memory address of the object.

Instance

The following example shows how to use id:

 a ='runoob'id(a)4531887632
 b =1id(b)140588731085608

So far, this article on the operation of id functions in python is introduced. For more information about how id functions of python work, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. .Cn!

Recommended Posts

How to run id function in python
How to use the round function in python
How to use the format function in python
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
Join function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How Python implements FTP function
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
Python| function using recursion to solve
How to use Python's filter function
How to use python's help function
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool
How Python implements the mail function
Is there function overloading in python
How to install PHP7.4 in CentOS
How to save the python program