Python Forum
Non-mutability of integer-valued variables.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Non-mutability of integer-valued variables.
#1
I’m reading a tutorial on mutability of lists. The gist of it is that when one list is set to be equal to another, they point to the same part of the computer’s memory, hence modifying one would also modify the other. This would not happen with integer-valued variables, as belows. Why is it so? When I type “cheese += 1”, what happens with respect to the reference stored in the variable, and the computer’s memory?

spam = 42
cheese = spam
cheese += 1
cheese 
43
spam
42
Reply
#2
cheese += 1 is short for cheese = cheese + 1, it is not increasing the cheese int value of 42 by 1
cheese is pointing at a int of 42, + int of 1 gives a result of an int of 43 which cheese now points to.
spam is left pointing to the original int 42
Reply
#3
You are confused about what python variables are. A python variable is a name that is associated with a thing. In python it is perfectly valid to do this:
myvar = [1, 2, 3]
print(myvar, type(myvar), id(myvar))
myvar = ['1', '2', '3']
print(myvar, type(myvar), id(myvar))
myvar = 123
print(myvar, type(myvar), id(myvar))
myvar = '123'
print(myvar, type(myvar), id(myvar))
Output:
[1, 2, 3] <class 'list'> 2285393208704 ['1', '2', '3'] <class 'list'> 2285393276864 123 <class 'int'> 140717799368160 123 <class 'str'> 2285393367152
Unlike a C variable that allocate space to store a thing (even if it is only a pointer to a thing), Python variables are a name that is give to a Python object. For the list of integers, "myvar" mapped to python object 2285393208704. Then I remapped "myvar" to 2285393276864 which is a list of strings. Lists are lists, so that isn't all that odd, but the next line maps "myvar" to 140717799368160 which is an integer. In C I could not reuse a list pointer variable as an integer variable.

I find it most useful to think of python variables as entries in a dictionary. A scope is a dictionary and the variables are keys in the scope dictionary.

So what does this have to do with mutability? Look at this example:
myvar = [1, 2, 3]
print(myvar, type(myvar), id(myvar))
myvar.append(4)
print(myvar, type(myvar), id(myvar))
myvar = 42
print(myvar, type(myvar), id(myvar))
myvar += 1
print(myvar, type(myvar), id(myvar))
Output:
[1, 2, 3] <class 'list'> 1551940838592 [1, 2, 3, 4] <class 'list'> 1551940838592 42 <class 'int'> 140717799365568 43 <class 'int'> 140717799365600
When myvar maps to a list, the mapping does not change when I append to the list. myvar is mapped to the same list before and after the append. The list changes, but the mapping of myvar does not.

Things are different when myvar is mapped to an integer. Adding 1 to the integer results in a new integer object. The ID changes! If we look at Python object 140717799365568, it is still an integer object 42. But myvar was remapped to a new integer object 43 (140717799365600). myvar had to be remapped because integer objects are immutable.

So mutability is not a variable thing, it is a python object thing. Some python objects, like the list object 551940838592 can be modified (they are mutable), and some python objects, like integer object 40717799365568, cannot be changed (they are immutable). What python does with the variable is the same. The variable gets mapped to the result of the operation. list.append(item) returns the original list, so myvar = myvar.append(item) does not change the mapping between myvar and the python list. 42 + 1 does not return the python object 42, or the python object 41. It returns a new integer python object 43. Thus myvar = myvar + 1 maps python to this new variable (43).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020