Methods and Functions

Python id()

In Python, every data element or object stored in the memory is been referenced by a numeric value. These numeric values are useful in order to distinguish them from other values. This is referred to as the identity of an object. The python id() function is used to return a unique identification value of the object stored in the memory. This is quite similar to how unique memory addresses are assigned to each variable and object in the C programming language. This article goes over the Python id() method, its syntax, and some examples.

Definition

  • The Python id() function accepts a single parameter and returns a unique identity (integer value) of that object.

Python id()

What exactly does the Python id() function do? When we input any object as a parameter into the Python id() function, the id() function assigns an identity to the object. This identity is an integer value which remains constant during the lifetime of the program and cannot be updated or modified.

Each object has its own unique identity assigned to it as an integer number which differentiates it from other objects.

Syntax –

where,

object = int, float, string, tuple, list, class, function, etc.

Note – The Python id() function only takes a single parameter object.

How id() works?

Python can utilize an object’s id to cache the values of its variables. This approach for accessing cached items via id() improves Python performance. As a result, many variables may refer to the same object and have the same id() value if their values are the same.

Basic Example –

                    

# Python program to illustrate id() function
a = 10
b = 11
c = 130.56
text = 'Hello'

print('ID of a =', id(a))
print('ID of b =', id(b))
print('ID of c =', id(c))
print('ID of text =', id(text))

Output –

                    

ID of a = 9781088
ID of b = 9781120
ID of c = 139832028993808
ID of text = 139894857394352

Notice in the above program, for the variables holding integer values 10 and 11, there is an identity difference of only 32.

Why so?

Understand that the id(11) is the next block after id(10) in the memory location. And since integer values in Python are 4 bytes i.e. 32 bits, which is the exact difference between the 2 ID numbers. This also means that Python stores integers in a sequence of blocks that are spaced equally. Hence providing an efficient memory allocation process.

More Examples on id()

  1. An instance where ID’s are unidentical –

Example –

                    

# Python program to illustrate id() function
a = 10
b = 14
text1 = 'Hello'
text2 = 'Hi'

print(id(a) == id(b))
print(id(text1) == id(text2))

Output –

Here the output is FALSE in both the print statements because each variable has a different value assigned to it, and the Python id() function will allocate a unique identification number to each of them.

  1. An instance where ID’s are identical –

There are few cases when the Python id() function assigns the exact same identification number to multiple objects. This happens when the objects, like integers, floats, strings, tuples are immutable. The id() function works on the principle of caching which in turn works only on the objects that are immutable. This helps Python preserve memory.

Example –

                    

# Python program to illustrate id() function
txt1 = 'Python'
txt2 = 'Python'

my_tuple1 = ('A', 'B', 'C')
my_tuple2 = ('A', 'B', 'C')

print(id(txt1))
print(id(txt2))

print(id(my_tuple1))
print(id(my_tuple2))

Output –

                    

140562250770160
140562250770160
140562250322496
140562250322496

  1. Python id() function on Immutable objects –

On immutable objects like lists and dictionaries, the id() function allocates distinctive identity numbers.

Example –

                    

# Python program to illustrate id() function
my_list1 = [1, 2, 3, 4]
my_list2 = [1, 2, 3, 4]

my_dict1 = {1: 'Hi', 2: 'Hello'}
my_dict2 = {1: 'Hi', 2: 'Hello'}

print(id(my_list1))
print(id(my_list2))
print(id(my_dict2))
print(id(my_dict2))

Output –

                    

140117770739136
140117770107456
140117770935360
140117770935360

Using id() on Custom Objects

Python id() function can also be used on user-defined classes. Let’s take a simple example to understand this.

Example –

                    

# Python program to illustrate id() function in class
class Car:
     name = 'Ford'
     price = 30000

c1 = Car()
print(id(c1))

c2 = Car()
print(id(c2))

c3 = Car()
c3.name = 'Audi'
print(id(c3.name))

Output –

                    

140030909930752
140030909344928
140030908951088

Frequently Asked Questions

Q1. What is the id in Python?

The ‘id’ in Python stands for Identity. Each and every object in Python when stored into the memory is being allocated a unique identification number that helps the Python compiler to perform better and utilize memory efficiently. Each object has its own unique identity assigned to it as an integer number which differentiates it from other objects.

Q2. What does id() do in Python?

The Python id() function accepts a single parameter and returns a unique identity (integer value) of that object. When we input any object as a parameter into the Python id() function, the id() function assigns an identity to the object. This identity is an integer value that remains constant during the lifetime of the program.

Example –

                    

# Program to illustrate Python id() function
a = 12.46
txt = 'Hello World'
items = ['soap', 'brush', 'deodorant']

print(id(a))
print(id(12.46))
print(id(txt))
print(id(items))

Output –

                    

139996850014480
139996850014480
139996849567984
139996850200128

Note – Here, the id for variable a and float value 12.46 is the same. This is because both are sharing the same values and hence Python id() function assigns them with the equivalent identity number.

Q3. What is the return type of id()?

The syntax for Python id() function is as below –

where,

object = int, float, string, tuple, list, class, function, etc.

The id() function in Python returns every object’s “identity.” It returns a unique integer value that remains constant throughout the program execution time and cannot be modified or changed.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.