Why are integers immutable in Python?

I understand the differences between mutable and immutable objects in Python. I read a lot of posts discussing the differences. However, I have not read anything about WHY integers, which are immutable objects.

Is there a reason for this? Or the answer "is this just what is"?

Edit: I am being asked to “distinguish” this question from other questions, as it seems to be a previously asked question. However, I believe that what I ask is more likely to be related to the philosophical question of Python than to the technical question of Python.

It seems that the "primitive" objects in Python (i.e. strings, booleans, numbers, etc.) are immutable. I also noticed that derived data types consisting of primitives (i.e. Dicts, lists, classes) are mutable.

Is the line the line is drawn on, is the object resized? Primitive and derivative?

+13
python immutability mutable
source share
2 answers

Executing integers that are mutable would be very controversial for how we are used to working with them.

Consider this piece of code:

a = 1 # assign 1 to a b = a+2 # assign 3 to b, leave a at 1 

After completing these assignments, we expect that the value 1 and b will have the value 3. The add operation creates a new integer value from the integer stored in a and the instance of the integer 2. If the add operation just took the integer in and just changed it , then both a and b will have a value of 3.

So, we expect that arithmetic operations will create new values ​​for their results - do not change their input parameters.

However, there are cases where a mutation in the data structure is more convenient and efficient. Suppose that at the time list.append(x) did not change list , but returned a new copy of list with the addition of x . Then a function like this:

 def foo(): nums = [] for x in range(0,10): nums.append(x) return nums 

just return an empty list. (Remember - here nums.append(x) does not change nums - it returns a new list with the addition of x . But this new list is not saved anywhere.)

We need to write the foo procedure as follows:

 def foo(): nums = [] for x in range(0,10): nums = nums.append(x) return nums 

(This, in fact, is very similar to the situation with Python strings up to about 2.6 or maybe 2.5.)

In addition, each time we assign nums = nums.append(x) , we will copy a list that grows in size, resulting in a quadratic behavior. For these reasons, we create lists of mutable objects.

The consequence of creating mutable lists is that after these statements:

 a = [1,2,3] b = a a.append(4) 

list b has changed to [1,2,3,4] . This is what we live with, despite the fact that it still turns us on from time to time.

+15
source share

What are the design choices to make numbers immutable in Python?

There are several reasons for immutability, let's see first what are the reasons for immutability?

1- Memory

  • Saves memory. If you know that the object is immutable, you can easily copy it by creating a new link to the same object.
  • Performance. Python can allocate space for an immutable object at creation time, and storage requirements are fixed and immutable.

2- Fast execution.

  • It does not have to copy every part of the object, just a simple link.
  • It’s easy to compare; comparing equality by reference is faster than comparing values.

3- Security:

  • In applications with multiple threads, Different threads can interact with data contained within immutable objects without worrying about data consistency.
  • The internal state of your program will be consistent, even if you have exceptions.
  • Classes should be immutable unless they have a good reason to make them mutable .... If a class cannot be made immutable, limit its variability to as much as possible

4- Ease of use

  • Easier to read, easier to maintain, and less likely to fail in an odd and unpredictable way.
  • Immutable objects are easier to test, not only because of their easy simulation, but also with the code templates that they usually use.

5- Keys must be immutable . This means that you can use strings, numbers or tuples as a dictionary. This is what you want to use.

 The hash table implementation of dictionaries uses a hash value calculated from the key value to find the key. If the key were a mutable object, its value could change, and thus its hash could also change. But since whoever changes the key object can't tell that it was being used as a dictionary key, it can't move the entry around in the dictionary. Then, when you try to look up the same object in the dictionary it won't be found because its hash value is different. If you tried to look up the old value it wouldn't be found either, because the value of the object found in that hash bin would be different. 

Returning to integers:

  • Security ( 3 ), easy to use ( 4 ) and the ability to use numbers in the form of dictionaries ( 5 ) are the reasons for deciding to make numbers unchanged.

  • Has fixed memory requirements since inception ( 1 ).

  • Everything in Python is an object, numbers (like strings) are "elemental" objects. No activity will change the value of 8 to anything else, and no activity will change the string "eight" to anything else. This is due to the fact that the solution is in design too.

+4
source share

All Articles