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

Python basic tutorial

Python flow control

Funções do Python

Tipos de Dados do Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

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 lifetime.

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 an object, even numbers and classes.

Therefore, integer5Has a unique ID. Integer5The id remains unchanged during the lifetime of the float.5.5And it is the same with other objects.

Python built-in functions