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)
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.
(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
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
id(a)
# 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
id(a)
# 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.
(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.
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