pythonmemory-management

Why does python allocate new memory to variables on every new assignment?


Why is it that when a variable is reassigned a new value in python new memory is allocated? Why can't modification take in place like it happens if an extra value is appended to a list, but when it is reassigned with the current list plus some new list, new memory is allotted.

>>> a=2
>>> id(a)
13332800
>>> a=a+2    # reassigning allocates new memory
>>> id(a)
13332752
>>> a=[1,2,3,4]
>>> id(a)
139923169899008
>>> a.append(2)  # change takes in place
>>> id(a)
139923169899008
>>> a=a+[3,2]    # this way causes new memory allocation
>>> id(a)
139923169899656

Is there any way to avoid reallocation of memory on every new assignment?


Solution

  • Python was designed to have integers be immutable objects. Meaning, you can never mutate (change) them and every operation with them yields a new integer object:

    >>> a = 2
    >>> id(a)
    505911000
    >>> id(a + 1)
    505911016
    >>> a += 2
    >>> id(a)
    505911032
    >>>
    

    Lists on the other hand were designed to be mutable objects. This means that you can change/update their contents without creating a new list object. Moreover, this is exactly what the list.append method does. It adds a new item to the list without creating a new object.

    This code however:

    a=a+[3,2]
    

    is different than list.append in that it does create a new list object because you used the + operator. a+[3,2] creates a new list object that is the combination of a and [3,2]. You then assign this new object to the name a, overshadowing the old list object and causing a to now refer to the new list. That is why id(a) returns a different number.