Introduction: Python id() Function
When working with Python, there are situations where it is useful to determine whether two variables refer to the same object. Although two objects may contain identical values, they are not always the same object in memory.
Without a way to identify objects, it becomes difficult to understand object references, variable assignments, and how Python manages objects during program execution.
This is where the Python id() Function provides a simple way to identify objects in Python.
What it is: The id() function is a built-in Python function that returns the unique identity of an object. The returned value is an integer that uniquely identifies the object during its lifetime.
The Python id() Function helps you determine whether two variables refer to the same object, making it useful for learning object identity and debugging Python programs.
Take a look at a quick example to see how it works.
You can also explore its real-world use cases to understand its practical applications in Python programming.
Now let’s understand its syntax, parameters, and return value before exploring practical examples.
💡 Tip: Want to explore more built-in functions like id()? Browse the Python Built-in Functions Learning Guide for categorized tutorials and practical examples.
Syntax, Parameters, Return Value and Examples: Python id() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python id() Function.
Syntax
id(object)
Parameters
| Parameter | Description |
|---|---|
object |
The object whose unique identity is to be returned. |
Return Value
| Return Value | Description |
|---|---|
| int | Returns an integer that uniquely identifies the specified object during its lifetime. |
Quick Example
The following example displays the unique identity of a variable using the id() function.
name = "Alice"
print(id(name))
# Sample Output:
140420392517936
The id() function returns an integer that uniquely identifies the object referenced by name. The actual value may differ each time the program runs.
How the Python id() Function Works
- The
id()function accepts an object as its argument. - It returns the unique identity of the object as an integer.
- The returned identity remains the same while the object exists.
- Different objects usually have different identities, even if they contain the same value.
- The returned value can be compared to determine whether two variables refer to the same object.
- It is commonly used for understanding object identity and debugging Python programs.
Examples: Python id() Function
The following examples show how the Python id() Function works in different programming scenarios.
Example 1: Finding the Identity of an Integer
number = 100
print(id(number))
# Sample Output:
140703282315280
Explanation: The id() function returns the unique identity of the integer object referenced by number. The actual value may differ on different systems or program executions.
Example 2: Finding the Identity of a String
name = "Alice"
print(id(name))
# Sample Output:
140703282421808
Explanation: The function returns the unique identity of the string object stored in the variable.
Example 3: Comparing the Identity of Two Variables
a = [10, 20, 30]
b = a
print(id(a))
print(id(b))
# Sample Output:
140703282550976
140703282550976
Explanation: Both variables refer to the same list object, so id() returns the same identity for each variable.
Example 4: Comparing Different Objects with the Same Value
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(id(list1))
print(id(list2))
# Sample Output:
140703282551104
140703282551424
Explanation: Although both lists contain the same values, they are different objects, so their identities are different.
Example 5: Checking Object Identity
x = {"name": "Alice"}
y = x
print(id(x) == id(y))
# Output:
True
Explanation: Since x and y reference the same dictionary object, their identities are equal.
Example 6: Identity After Assignment
value = 25
print(id(value))
value = 50
print(id(value))
# Sample Output:
140703282315280
140703282316880
Explanation: After assigning a new value, value refers to a different object, so id() returns a different identity.
Use Cases: When to use the id() Function
Below are some common situations where the Python id() Function becomes useful:
- Checking whether two variables refer to the same object.
- Understanding object references and variable assignments.
- Debugging programs that work with mutable objects.
- Learning how Python manages object identity.
- Verifying object identity while testing Python programs.
- Exploring Python’s object model during development.
Key Takeaways: id() Function
Before wrapping up, here are the key points to remember about the Python id() Function:
- The
id()function returns the unique identity of an object. - The returned value is an integer that uniquely identifies the object during its lifetime.
- Objects with the same value can have different identities.
- The returned identity can be compared to determine whether two variables reference the same object.
- It works with all Python objects.
- It is commonly used for learning object identity and debugging Python programs.
In short, the Python id() Function helps you understand how Python identifies objects, making it easier to work with object references and debug programs.