Python id() Function
The id()
method returns a unique integer (identity) of a passed argument object.
Since ID is an assigned memory address, it can be different in different systems. So, the output on your system can be different.
Syntax
id(object)
id() Parameters
Python id()
function parameters:
Parameter | Condition | Description |
---|---|---|
object | Required | Any Python object whose identity is required. It can be a class, variable, list, tuple, set, etc. |
id() Return Value
Python id()
function returns the identity of the object (which is a unique integer for a given object)
Examples
Example 1: Basic Examples
# id of 5
print("id of 5 =", id(5))
a = 5
# id of a
print("id of a =", id(a))
b = a
# id of b
print("id of b =", id(b))
c = 5.0
# id of c
print("id of c =", id(c))
output
id of 5 = 136596395200712
id of a = 136596395200712
id of b = 136596395200712
id of c = 136596384063184
Notice that we have used the id()
function with variables a
, b
and c
and got their corresponding ids: the id()
function returns the integer 136596395200712
for both a = 5
and 5
. This because both values are the same, and so the id
is also the same.
Example 2: id() function with Classes and Objects
class Food:
banana = 15
my_food = Food()
# id of the object my_food
print(id(my_food))
output
132413969081424
Example 3: id() function with Sets (Mutable Objects)
d1 = {"A": 1, "B": 2}
d2 = {"A": 1, "B": 2}
print(id(d1)) # Output: Different from id(d2) because dictionaries are mutable
print(id(d2))
output
134716606985664
134716607036672
Example 4: id() function with Tuples (Immutable Objects)
t1 = (1, 2, 3)
t2 = (1, 2, 3)
print(id(t1)) # Output: Same as id(t2) because they are the same object
print(id(t2))
output
139212483455616
139212483455616
Example 5: id() with Sets
colors = {"red", "yellow", "blue", "green"}
# id() of the set colors
print("The id of the colors set is", id(colors))
output
The id of the colors set is 133643641542400
Example 6: Different Identities for Different Instances
class MyClass:
pass
obj1 = MyClass()
obj2 = MyClass()
print(id(obj1)) # Output: Unique id for obj1
print(id(obj2)) # Output: Unique id for obj2
output
139946208764944
139946208765008
Example 7: Compare Two IDs using is
keyword
To check if two objects have same id, use is
keyword.
For example, check if 'x' and 'y' have the same id (i.e. if they point to the same object)
x = 42
y = x
print(x is y) # Output: True
output
True
is
keyword is used for identity (id) comparison, while ==
operator is used for equality (value) comparison.
Particular exceptions with id in Python
In Python, every object has its own unique id. However, for the sake of optimization, there are some exceptions.
Some objects have same id (actually one object with multiple pointer), like:
- Small integers between -5 and 256
- Python optimizes memory usage by caching a range of small integers, from -5 to 256. When you use these numbers, Python will always refer to the same memory address. For example,
id(-5)
andid(256)
will give you the same ID because they are both cached
- Python optimizes memory usage by caching a range of small integers, from -5 to 256. When you use these numbers, Python will always refer to the same memory address. For example,
- Small interned strings (usually less than 20 character)
- Python creates a pool of immutable objects called "interned strings". When you create a string that is already in this pool, Python will return a reference to the existing object rather than creating a new one. This applies to strings of length 0 to 20 characters. So, if you create a string with the same content as an existing interned string, it will have the same ID.
print(30 is 20+10) # Output: True
print('aa'*2 is 'a'*4) # Output: True
output
True
True