Why are str and None immutable?

Please answer the questions

Jun.06,2022

first correct: str is a class (or you can call it a type), while None is an object of the NoneType class

In short, immutability means that you cannot change its value. It goes without saying that None is an object of the NoneType class and cannot modify its value; for objects of the str class, the str class does not provide any methods to modify the value of an object, and those methods that modify the value are newly generated a str object.

therefore, these objects are immutable.

< hr >

the following is a description of the official document:

An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary.

floor is right, but I would add that when you modify a string, simply put, python virtual machine will have two actions: (1). Create a new string (the one you need, the one you modify based on the original string), (2). Then pass the reference (equivalent to C pointer) of your new string to the string variable. I don't know if I've made it clear. You can refer to the book python Source Code Analysis

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eadf6d-2199.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eadf6d-2199.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?