Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

id() Function in Python

Last Updated on February 25, 2023 by Prepbytes

Every data element or object in memory is referenced by a numeric value in Python. These numerical values are useful for distinguishing them from other values. This is what is meant by an object’s identity. The id() method in Python is used to return a unique identification value for an object that is stored in memory. This is somewhat similar to how each variable and object in the C programming language is assigned a unique memory address. This article discusses the id function in python, its syntax, and some examples.

What is id() Function in Python

id() is a built-in Python method that returns a unique id for the given object, and this id must be unique throughout the object’s lifespan.
In Python, each object and variable has a unique id. Consider this to be similar to the process Ids used by operating systems to uniquely identify each process. When a Python object is saved in memory, it is assigned a unique identification number, which helps the Python interpreter run better and use memory more efficiently. The id() function returns the object’s unique identification. The id() function in Python does not require any further installation; it comes standard with any Python installation (e.g., Anaconda, Python IDLE, etc.).

This method can be called without using any additional import statements. When an object is created in memory, it is allocated an ID. The ID is the memory address of the object, and it will be different each time you execute the program. except for some objects, such as integers from -5 to 256, which have a constant unique id.

Syntax of id() Function in Python

The syntax of the id() function is:

id(object)

where,

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

Let’s have a look at how id function in python works:

prepBuddy = 25
print(prepBuddy)
print(id(prepBuddy))

Output:

25
22761063107632

Here, a variable prepBuddy was created and the value 25 was stored. The value returned by id() for the variable prepBuddy is its unique id. Only the variable prepBuddy can hold that id.

Parameters of id() Function in Python

The object parameter is the only parameter passed to the id() function. The object parameter can be anything from constants and variables to a Python-created custom class object.

Return Value of id() Function in Python

The id() function returns a unique id that is held only by the specified object. id() function will always return an integer number as the unique id.

prepBuddy = 12
print(prepBuddy)
print(id(prepBuddy))
print("Return type of id() : ", type(id(prepBuddy)))

Output:

12
22865930246800
Return type of id() : 

Examples of id() Function in Python

Let’s look at some examples of how to use the id function in Python:

Example 1
Here’s an example where the id() function returns identical values for two objects in Python:

x = 'PrepBytes'
y = 'PrepBytes'

print(id(x)) 
print(id(y))  

print(id(x) == id(y))

Output:

22457225599472
22457225599472
True

Explanation – In this example, we create two string objects x and y, both of which have the same value of ‘PrepBytes’. Since strings are immutable in Python, the value of the object cannot be changed, and Python optimizes memory usage by sharing the same object in memory when two variables contain the same value. As a result, the id() function returns the same memory address for both x and y, and the equality comparison returns True.

Example 2
Here’s an example of using the id() function on immutable objects in Python

my_list1 = [12, 24, 31, 49]
my_list2 = [12, 24, 31, 49]

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

print(id(my_list1)==id(my_list2))
print(id(my_dict1)==id(my_dict2))

Output:

False
False

Example 3
Here’s an example of using the id() function on custom objects in Python:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Manoj", 21)
person2 = Person("Ankit", 25)

print(id(person1)) 
print(id(person2))

Output:

23142764242208
23142764244416

Explanation – In this example, we define a Person class that has two attributes: name and age. We then create two instances of the Person class: person1 and person2. When we pass these objects to the id() function, it returns the memory address of the object referred to by each variable.

Since person1 and person2 are distinct objects in memory, they have different memory addresses, and the id() function returns a different integer for each object.

Summary

  • The id() function returns a unique identifier for an object in Python.
  • The identifier is an integer that represents the memory address of the object in the computer’s memory.
  • The syntax of the id() function is id(object), where object is the object for which you want to retrieve the identifier.
  • The id() function can be used on any Python object, including integers, floats, strings, lists, tuples, dictionaries, functions, classes, and instances of classes.
  • The identifier returned by id() is read-only and cannot be modified.

FAQs Related to id() Function

Here are some frequently asked questions about the id function in Python:

Q1 – What does the id() function do in Python?
A: The id() function in Python returns a unique identifier for an object. This identifier is an integer that represents the memory address of the object in the computer memory.

Q2 – What is the syntax of the id() function?
A: The id() function takes a single argument, which is the object for which you want to retrieve the identifier. The syntax is: id(object)

Q3 – What type of objects can the id() function be used on?
A: The id() function can be used on any Python object, including integers, floats, strings, lists, tuples, dictionaries, functions, classes, and instances of classes.

Q4 – Can the id() function be used to compare two objects for equality?
A: No, the id() function should not be used to compare two objects for equality. While the identifiers returned by id() are unique for each object, they are not guaranteed to be consistent across different runs of the program or different machines. To compare two objects for equality, you should use the == operator instead.

Q5 – Can the id() function be used to modify the memory address of an object?
A: No, the id() function does not allow you to modify the memory address of an object. The identifier returned by id() is read-only and cannot be modified.

Q6 – Is it safe to use the id() function to track objects in a program?
A: While the id() function can be used to track objects in a program, it is not recommended to rely on this method for complex programs. The identifier returned by id() can be affected by garbage collection, memory fragmentation, and other factors, which can make it difficult to use for long-term tracking. Instead, you should use more robust techniques for tracking objects, such as reference counting or garbage collection.

Leave a Reply

Your email address will not be published. Required fields are marked *