In this post, we will discuss What is the pointer in Python? Does a pointer exist in Python?

Variables in pythons are pointers (for all but the basic most types). The assignment operator “=” automatically creates a pointer unless it is a basic type eg. int, float, str, bool (These are not pointers).

lists, dicts, class, and objects, etc are pointers.

If you have a variable “l” representing a list (which is not a basic type).

The above statement creates the list object and also creates a pointer to it called “l”.

So if you assign m=l both m and l points to the same list object. Therefore if you change m[0] then l[0] also changes.


List as pointers in python

You can check that both m and l points to the same object using “is” operator. Python “is” operator is not the same as “==” operator because “==” operator compares two different objects and gives True as long as the contents are the same.

Output: True

Also, it is important to note that python deletes the object only when all the pointers to the object is deleted.

So if you del m or del l python will store the object in the memory unless all the pointers pointing to the list object are deleted.

Simples types are not pointers. To prove that examine the following code.


python output

Notice changing the value of “a” doesn’t change “b”.

Thus concluding that python treats every variable or objects as pointers except the basic types.

Comment below if you have any doubts or suggestions.

Leave a Reply