[Python] Why immutable int types can be changed to different values

Conclusion first

If you assign a different value to an int type variable, the storage location will change. In other words, the value of the int type variable cannot be changed while using the same address. (Conversely, mutable lists can do that)

Reason for writing the article

There are already many articles that touch on the behavior of mutable / immutable. However, if a python beginner searches for a question like this article as it is, I thought that it would be like "python int immutable can be changed", so I created it so that I can find the information I want by that search method.

What is mutable / immutable?

(There are many easy-to-understand articles for detailed descriptions, so please refer to them.)

--mutable: Types whose values can be changed after variable definition (list, dict, etc.) --immutable: immutable types (int, str, etc.)


It's said that it can't be changed, but you can reassign it after defining both int and str!

#Define once
var_int = 2
#You can reassign it!
var_int = 5

What is actually happening

Use the id function installed in python to check the id (address) of the variable. Usage environment: python 3.8.3, jupyter notebook

#Assign the int type number 3 to the variable a
a = 3
# id()Get the address of variable a using
# out: 4500765184

As you can see, the id of the variable a is 4500765184. Next, let's change the value.

#Assign the int type number 2 to the variable a
a = 2 #You can change it!
# id()Get the address of variable a using
# out: 4500765216
#If you think, the id has changed!

In this way, you can change the value of the same variable at first glance, so it looks like it is not immutable. However, if you check the id, you can see that the id has been changed even with the same variable name.

In other words, if you reassign the int value to the same variable name, it will be stored in the new address! Since the value of the original address cannot be changed, it is immutable.

A little deeper about id

(For those who are interested because I have already finished what I want to convey in the article) If you check docs about python id,

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

Is written. In particular, in the case of CPython, it is written that it represents a memory address.

Most of the Python we are using now is an implementation called CPython, so basically you can think of the id as a memory address.

First of all, an object is like a box containing variables. Alternatively, it may be easier to understand the address where the variable is stored. Since the program runs on a computer, "what value" and "where to remember" are required. The object is considered to be a group of these.

Then, if you reassign the value to an immutable variable, a different object will be created. In other words, a different address will be secured and a new value will be stored.

At the end

I remembered my doubts when I was a beginner, so I summarized them. To be honest, it may be rare to care about the storage location of variables in python, but I hope it will help solve the question. If you have any mistakes, I would appreciate it if you could teach me!

Recommended Posts

[Python] Why immutable int types can be changed to different values
Have python check if the string can be converted / converted to int
Only size-1 arrays can be converted to Python scalars
Python immutable type int memo
New features in Python 3.9 (1)-Union operators can be used in dictionary types
[Python] How to swap array values
[Raspberry Pi] Changed Python default to Python3
ValueError: only one element tensors can be converted to Python scalars workaround