The
id()
function in Python is a built-in function that returns the unique identity of an object. This identity is an
integer
that is guaranteed to be unique and constant for the object during its
lifetime
.
The
id()
function can be used to determine whether
two variables refer to the same object in memory
. This is helpful when determining the
type
of copy you have with two variables: if they refer to the same
id()
number then they are referencing the
same object
, on the other hand if they refer to two distinct
id()
numbers then while the objects may contain the
same content
they are different objects.
Here’s a quick example demonstrating this concept using lists:
>>> list1 = [1, 2, 3]
>>> id(list1)
140628379671424
>>> list2 = [1, 2, 3]
>>> id(list2)
140628380479552
This Python code consists of several lines that perform the following operations:
-
list1 = [1, 2, 3]
: Creates a list namedlist1
variable that contains a simple 3 integer list: 1, 2, and 3. -
id(list1)
: Theid()
function is called withlist1
as its argument. It returns the unique identifier (memory address) for thelist1
object, an integer value. In this case, the memory address is140628379671424
. (Note that the memory address will vary each time you run the code, depending on where Python allocates memory for the list.) -
list2 = [1, 2, 3]
: Creates another list namedlist2
, which also contains 3 integer elements: 1, 2, and 3. -
id(list2)
: Theid()
function is called again, this time withlist2
as its argument. It returns the unique identifier (memory address) for thelist2
object, which is an integer value. In this case, the memory address is140628380479552
. As mentioned before, the actual memory address will vary each time you run the code, depending on where Python allocates memory for the list.
This code creates two separate lists,
list1
and
list2
, containing the same elements (1, 2, and 3). Although the elements of
list2
are the
same
as those in
list1
, these are
two separate list objects
stored in
different
memory locations.
This means when an object is created in Python, it is assigned a unique integer identifier that is used to distinguish it from other objects. The identity is based on the object’s memory address, a unique location in the computer’s memory and the
id()
function returns this memory address as an integer.
But what does it mean when two variables have the same memory address?
Have a look at the following example:
>>> list1 = [1, 2, 3]
>>> id(list1)
140628380473280
>>> list2 = list1
>>> print(list2)
[1, 2, 3]
>>> id(list2)
140628380473280
Notice the example above is almost very similar to the previous example, except for one minor change:
list2 = list1
. In the previous example
list2
was assigned a list of integers, but in this example it is assigned to the
list1
variable.
And notice the contents of
list2
are the same:
[1, 2, 3]
.
But see how this time the
id(list2)
does not return a distinct memory address
different
from the
id()
returned by
list1
– this time, it’s the same.
This means if we continue with the above code by modifying the list it will affect the results of the other:
>>> list1 = [1, 2, 3]
>>> id(list1)
140628380473280
>>> list2 = list1
>>> print(list2)
[1, 2, 3]
>>> id(list2)
140628380473280
>>> list1.append(100)
>>> print(list2)
[1, 2, 3, 100]
Notice how the appending of
100
was performed on the variable
list1
, and yet when you print
list2
, it produces a list with
100
appended to it.
This is what happens when you operate on variables that refer to the same object – whatever is performed through one variable will affect the result of the other variable too.
Remember: if the
id()
of two variables is the same, they refer to the same object in memory. This can be useful when you want to avoid creating duplicate objects and is handy when passing a variable around in multiple functions.
id()
Examples
The
id()
function is often used in conjunction with the
is
operator to compare two objects. The
is
operator compares the identity of two objects, while the
==
operator compares their values.
For example, if
x
and
y
are two objects,
x is y
will return
True
if
x
and
y
have the same identity, while
x == y
will return
True
if
x
and
y
have the same value.
Here is an example demonstrating this concept:
>>> text1 = "Hello, world!"
>>> text2 = "Hello, world!"
>>> id(text1)
140628380315504
>>> id(text2)
140628376773936
>>> text1 == text2
True
>>> text1 is text2
False
As you can see from the above code, two distinct variables are created
text1
and
text2
which contain the same content
"Hello, world!"
.
Each variable has its memory address output using the
id()
function.
The first check using
==
comparison returns
True
because both variables contain the same
value
.
However, when the comparative operator
is
is used the result is
False
because both variables
reference
different objects in memory.
The same would apply even with the previous example using lists, as demonstrated below:
>>> list1 = [1, 2, 3]
>>> list2 = [1, 2, 3]
>>> id(list1)
140628380545344
>>> id(list2)
140628380476800
>>> list1 == list2
True
>>> list1 is list2
False
Can
id()
Change?
The short answer is yes . The memory address of an object in Python can change during its lifecycle. Python uses a dynamic memory allocation system, where objects are created and destroyed as needed. When an object is created, Python assigns it a memory address, which can change if the object is modified or moved in memory.
Here are a few examples of how the memory address of an object can change in Python:
- When a list is extended, the memory address of the list may change if the list needs to be reallocated in memory to accommodate the new items.
- When a dictionary is updated, the memory address of the dictionary may change if the dictionary needs to be resized to accommodate the new key-value pairs.
- When the garbage collector runs, the memory address of any object that is moved in memory may change.
While the memory address of an object can change during its lifecycle in Python, this is not something that most Python developers need to worry about. Python’s memory management system is designed to handle these changes automatically, so developers can focus on writing code without worrying about memory management.
id()
Summary
In conclusion, the
id()
function in Python is a built-in function that returns the identity of an object. This identity is a unique integer that represents the object’s memory address. The
id()
function is useful in determining whether two variables refer to the same object in memory.
While the
id()
function can be useful in certain situations, it should be used with caution. It is important to note that the identity of an object can change during the lifetime of the program, particularly if the object is mutable. Additionally, the
id()
function should not be used as a substitute for equality testing. Two objects with different identities can still be considered equal if their values are the same.