Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report