English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python id() usage and examples

Python built-in functions

The id() function returns the identity of the object (unique integer).

The syntax of id() is:

id(object)

id() parameter

The id() function takes a single parameterobject.

id() return value

The id() function returns the identity of an object. It is an integer, unique for the given object, and remains unchanged throughout its lifecycle.

Example1How does id() work?

class Foo:
    b = 5
dummyFoo = Foo()
print('dummyFoo's id =', id(dummyFoo))

When you run the program, the output will be similar to:

dummyFoo's id = 140343867415240

More examples of id()

print('5The id =', id(5))
a = 5
print('a's id =', id(a))
b = a
print('b's id =', id(b))
c = 5.0
print('c's id =', id(c))

When you run the program, the output will be similar to:

5The id = 1453124160
a's id = 1453124160
b's id = 1453124160
c's id = 42380816

It is important to note that all content in Python is objects, even numbers and classes.

Therefore, integers5Have a unique ID. Integers5The id remains unchanged during the survival period. Floating point numbers5.5And so on with other objects.

Python built-in functions