Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between mutable objects and immutable objects in python

Shulou Source: shulou.com Published: 2022-05-31 10:43:11 09月29日 Update

This article mainly introduces what is the difference between mutable objects and immutable objects in python, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on the difference between mutable objects and immutable objects in python. Let's take a look.

Variable data types: list list and dictionary dict Variable data type allows the value of a variable to change, that is, if the operation of append, + =, etc. on a variable only changes the value of the variable without creating a new object, the address of the object referenced by the variable will not change, but for different objects with the same value, there will be different objects in memory, that is, each object has its own address, which is equivalent to saving multiple copies of the object with the same value in memory. There is no reference count, it is a real object.

Immutable data types: integer int, floating point float, string string, and tuple tuple. Immutable data type, does not allow the value of the variable to change, if you change the value of the variable, it is equivalent to a new object, while for an object of the same value, there is only one object in memory. There is an internal reference count to record how many variables refer to the object.

# for immutable objects, only one copy of a = (1Magne1) b = (1Magne1) print (astatabb) # trueprint (an is b) # id is the same, true# for variable objects, the id of each object is different, and objects with the same value keep multiple copies a = [1magin1] b = [1mem1] print (astatb) # trueprint (an is b) # id is different This is the end of false's article on "what's the difference between mutable objects and immutable objects in python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the difference between mutable objects and immutable objects in python". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Object variable same memory data type different knowledge change content address article value just only dictionary character string simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Xiaomi MySQL MariaDB