1

We can check if the object type is mutable or not by comparing the new memory location of modified variable with the originally defined variable.

For example - int() is an immutable object in Python. If I try to modify the value of integer type variable, I notice the memory location changes [Code and Output below]. Can someone provide a brief explanation going in the background ?

#initial variable
a = 10
# initial memory location
print(id(a))
#modified variable
a += 1
# new memory location, is it same?
print(id(a))
OUTPUT

93285870446416
93285870446524
Chitranjan
  • 47
  • 8
  • 6
    You can not modify an immutable object. That is what immutable means. The only thing you can do is to replace it with an other object. – Klaus D. Aug 23 '20 at 13:00
  • @KlausD. I understand we cannot modify immutable objects. But as in the above example, and in terms of python variables. Please explain as how is the memory changing takes place in background whenever I update an integer/string/float/tuple/bool type variable(s). **See mentioned code for reference** – Chitranjan Aug 23 '20 at 13:07

2 Answers2

7

What's going on in the background?

a = 10
  1. The interpreter sees an expression and determines the type of the result, which is int
  2. Memory for an int is reserved, giving a memory address, marking that memory as "in use"
  3. The result of the operation is stored at that memory address
  4. -/- (not applicable yet)
a += 1
  1. The interpreter sees an expression and determines the type of the result, which is int
  2. Memory for an int is reserved, giving a new memory address, because the old one is still in use
  3. The result of the operation is stored at that new memory address
  4. There is no variable poining to the old address any more, so that one is marked as free

Maybe another

a += 1
  1. The interpreter sees an expression and determines the type of the result, which is int
  2. Memory for an int is reserved, giving a either another new memory address, or reuse the now freed memory address ("old" address)
  3. The result of the operation is stored at that new memory address
  4. There is no variable poining to the address any more, so that one is marked as free

and so on

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Thanks @Thomas. That is a clear cut explanation. Makes sense to me. Please up vote the post as many will find your answer quite helpful. – Chitranjan Aug 23 '20 at 15:02
  • Does Python keep track of the created immutable objects? For example, below x and y have the same id, why? x="hello" y="hello" – Hikmat Farhat Sep 11 '21 at 14:24
  • 1
    @HikmatFarhat: this is not related to immutable objects, but constants. See this: `x = "hello"; y = "hello"; print("x", x, id(x)); print("y", y, id(y)); x = (x + "a")[0:-1]; y = (y + "a")[0:-1]; print("x", x, id(x)); print("y", y, id(y));` As long as it's constant, the IDs are identical. However, 2 identical immutable objects needn't have the same ID. (I use ; for line break) – Thomas Weller Sep 11 '21 at 14:57
  • @ThomasWeller Do you have a reference for that behavior? I find it strange. For example. ```x=2;y=3;x+=1;``` Now x and y have the same id – Hikmat Farhat Sep 11 '21 at 17:18
  • 1
    @HikmatFarhat: see https://stackoverflow.com/questions/69145663/is-the-small-number-cache-defined-in-python-specification-or-is-it-an-implementa – Thomas Weller Sep 11 '21 at 18:24
1

Note that contrarily to C, and static-typing languages, Python type-checks only at runtime.

in C you would do:

int a; /* creating a memory space allowing only holding data of integer type */
a = 3; /* storing the value 3, not a character, but an integer, otherwise error is returned at compilation */

in Python, a = 3 first creates an object 3 at a certain memory space, and then links the name a to that object location during assignment. a is then bound to that object by pointing to its memory allocated.

CPython already allocates a memory space for integers in range -5 to 256. If you write now b = 3, b will share the same memory location as b is simply another pointer to the same object 3.

Integers being immutable, if you do a += 1, then a new object 4 is created at a different memory address and then a will points to this new location. b stays bound to the object 3.

Note that if an object is not linked anymore to any names (a, b, etc), then it can be garbage-collected. A counter sys.getrefcount(X) is used to keep track of all the references to a given object "X".

Luc Bertin
  • 326
  • 1
  • 12