id() Builtin Function
Python id()
Python id() builtin function takes an object as argument and returns its id value. id is an integer which is constant and unique for an object during its lifetime.
In this tutorial, you will learn the syntax of id() function, and then its usage with the help of example programs.
Syntax
The syntax of id()
function is
id(object)
where
Parameter | Description |
---|---|
| Any valid Python object. |
Examples
1. Get id of a string object
In the following program, we take a string object in variable x
, get its id, and print it to standard output.
Python Program
x = 'hello world'
x_id = id(x)
print(x_id)
Output
4339760944
2. Get id of a list object
In the following program, we take a list object in variable x
, get its id, and print it to standard output.
Python Program
x = [5, 7, 0, 4]
x_id = id(x)
print(x_id)
Output
4371990784
3. No two objects will have same id
In a given lifetime of an object, it has unique id.
In the following program, we take two objects in variables x
and y
, get their id values, and print them to standard output. They will be different no matter how many times you run the program, because, id() function guarantees their uniqueness.
Python Program
x = [5, 7, 0, 4]
y = 'hello world'
x_id = id(x)
y_id = id(y)
print('x id :', x_id)
print('y id :', y_id)
Output
x id : 4379396352
y id : 4379410224
Summary
In this tutorial of Python Examples, we learned the syntax of id() builtin function, and how to read id of an object, with examples.