Python id()

Python id() function returns a unique integer identifier for an object during its lifetime. This identifier represents the memory location where the object is stored, making it useful for checking if two variables reference the same object. Python id() is particularly helpful for debugging and understanding object identity in memory management.
Table of Contents

Understanding Python id() Function

The id() function in Python is a built-in function that provides you with a unique identifier for a given object. This identifier is an integer. This is guaranteed to be unique and constant for the object during its lifetime. Think of it like a memory address, although the specific interpretation can vary depending on your Python implementation. Python id() helps check if two variables refer to the same object in memory.

Syntax of Python id()

identity_number = id(object)

Explanation

  • identity_number: Variable stores unique integer.
  • id(): Built-in function.
  • object: Object whose ID you want.

Example of Python id()

my_string = "hello"
string_id = id(my_string)
print(string_id)

Explanation

  • my_string = "hello": Assigns the string “hello”.
  • string_id = id(my_string): Gets the unique ID of my_string.
  • print(string_id): Displays integer.

Output

139934322073456


id() Parameters

The id() function takes a single parameter: the object whose unique identifier you want to retrieve. This object can be anything: a simple variable, a list, a function, or even a class. You only provide the object itself. Python id() then does the internal work to get the identifier.

Syntax

object_id = id(any_object)

Explanation

  • object_id: Variable to hold the returned ID.
  • id(): Retrieves the unique identifier.
  • any_object: The object.

Example

my_list = [1, 2, 3]
list_id = id(my_list)
print(list_id)

Explanation

  • my_list = [1, 2, 3]: Creates list.
  • list_id = id(my_list): Calls function to retrieve the ID of my_list.
  • print(list_id): Prints the ID.

Output

139934322524864


id() Return Value

Python id() function provides a single integer value. This value represents the unique identity of the object you passed in as a parameter. Remember that this ID is only guaranteed to be unique during the object’s lifetime. Once an object is no longer used, its ID might be reused for another object later. Two objects with non-overlapping lifetimes may have the same id() value.

Syntax

unique_integer = id(object_in_question)

Explanation

  • unique_integer: Will store returned integer.
  • id(): Python built-in function.
  • object_in_question: Object which id need to be calculated.

Example

x = 10
identity = id(x)
print(identity)

Explanation

  • x = 10: Assigns the value 10.
  • identity = id(x): Obtains integer using the id() function.
  • print(identity): Prints returned integer.

Output

139934588347696


Python id() for Inbuilt Datatypes

When you use Python id() with built-in data types like integers, strings, or floats, you’ll observe interesting behavior related to how Python manages these objects in memory. Python often uses a technique called “interning” for small integers and some strings. This means multiple variables with the same value point to the same object in memory, saving space. That is why their id() values will be identical.

Syntax

identifier = id(builtin_datatype_variable)

Explanation

  • identifier: This is the integer.
  • id(): Function call.
  • builtin_datatype_variable: Variable of type like int, string, float.

Example

a = 5
b = 5
print(id(a))
print(id(b))

Explanation

  • a = 5 and b = 5: Creates two variables with the same value.
  • print(id(a)): Prints the ID of a.
  • print(id(b)): Prints the ID of b.

Output

139934588347536
139934588347536


Python id() with Classes and Objects

When working with custom classes and objects in Python, id() provides the unique identity of each instance of your class. Each time you create a new object using your class, that object will have its unique ID. Even if two objects have the same attribute values, their IDs will differ as they are distinct instances residing in different memory locations.

Syntax

object_identity = id(instance_of_class)

Explanation

  • object_identity: Stores the ID.
  • id(): Used to obtain the ID.
  • instance_of_class: Object created from a custom class.

Example

class MyClass:
    pass

obj1 = MyClass()
obj2 = MyClass()
print(id(obj1))
print(id(obj2))

Explanation

  • class MyClass:: Defines a simple class.
  • obj1 = MyClass() and obj2 = MyClass(): Creates two objects of MyClass.
  • print(id(obj1)): Prints the ID of obj1.
  • print(id(obj2)): Prints the ID of obj2.

Output

139934322535536
139934322535600


Python id() with Sets

Sets are mutable collections of unique items in Python. Each set has a distinct identity as determined by Python ID (). If you create two sets, even if they initially contain the same elements, they will have different IDs. If you modify one set by adding or removing elements, its ID may remain the same or may change; it depends on the Python implementation.

Syntax

set_id = id(set_variable)

Explanation

  • set_id : Will store returned integer value.
  • id(): Built-in function.
  • set_variable: Set object.

Example

set1 = {1, 2, 3}
set2 = {3, 2, 1}
print(id(set1))
print(id(set2))

Explanation

  • set1 = {1, 2, 3} and set2 = {3, 2, 1}: Creates two sets.
  • print(id(set1)): Prints set1‘s ID.
  • print(id(set2)): Prints set2‘s ID.

Output

139934322536128
139934322535872


Python id() with Tuples

Tuples, unlike sets, are immutable in Python. You can check their identity. If you create two tuples with identical elements in the same order, then it may or may not show the same ID; it depends because Python may return the same ID for optimization of small-sized tuples. Python id() confirms this behavior.

Syntax

tuple_id = id(tuple_variable)

Explanation

  • tuple_id: Stores the unique identifier.
  • id(): Function to retrieve the ID.
  • tuple_variable: Variable holding a tuple.

Example

tuple1 = (1, 2, 3)
tuple2 = (1, 2, 3)
print(id(tuple1))
print(id(tuple2))

Explanation

  • tuple1 = (1, 2, 3) and tuple2 = (1, 2, 3): Initializes two tuples.
  • print(id(tuple1)): Gets ID of the tuple1.
  • print(id(tuple2)): Gets ID of the tuple2.

Output

139934322068864
139934322068864


Conclusion

Python id() function is used to get the unique identity of a Python object. The function takes one parameter: the object. The Python id() function return value is an integer. This integer is unique until the object is alive. You will become familiar with using Python id() with different datatypes.


Also Read

Python input()

Python isinstance()


Python Reference

Python id()