Introduction to mutable and immutable objects in Python

Introduction to mutable and immutable objects in Python

All data in Python can be treated as objects. In Python, everything is an object, including built-in data types such as integers, floating point numbers, strings, lists, and dictionaries, as well as custom classes and objects.

Each object has its own properties and methods, which can be accessed and called through the dot operator. For example, integer objects have properties such as size, number of digits, etc., and methods such as convert to string, sum, etc. String objects have attributes such as length, character index, etc., and methods such as splicing, case conversion, etc.

Objects in Python are divided into mutable and immutable

Every object in Python has its own identity, type, and value. Once an object is created, its identity never changes; you can think of it as the address of the object in memory. The "is" operator can compare whether the identification numbers of two objects are the same; the id() function can return an integer representing its identification.

Some objects' values ​​can change. Objects whose values ​​can be changed are called mutable; objects whose values ​​cannot be changed are called immutable. (If an immutable container object contains a reference to a mutable object, when the value of the latter changes, the value of the former will also change; but the container is still an immutable object because the set of objects it contains will not change. . Therefore, immutability does not strictly mean that the value cannot be changed. The actual meaning is more subtle.) The mutability of an object is determined by its type; for example, numbers, strings, and tuples are immutable, while dictionaries and lists are mutable. https://docs.python.org/zh-cn/3/reference/datamodel.html#objects-values-and-types

An object of mutable type can be modified after creation, and its value can change, but the identity of the object remains unchanged. Common variable types include lists, dicts, sets, etc. For example, we can change the value of a list by modifying its elements.

my_list = [1, 2, 3]

my_list[0] = 4

print(my_list) # Output [4, 2, 3]

Immutable (immutable) type objects cannot be modified after creation, and any modification operation will return a new object. Common immutable types include integers (int), floating point numbers (float), strings (str), tuples (tuple), etc. For example, we cannot directly modify a character of a string, but can only create a new string object through operations such as slicing and splicing.

my_string = "Hello"

new_string = my_string + " World"

print(new_string) # Output "Hello World"

It should be noted that the mutability of an object refers to whether the object itself can be modified, not whether the variables referenced by the object can be modified. For immutable type variables, we can reassign it to a new object, but the value of the original object remains unchanged.

my_string = "Hello"

my_string = my_string + " World"

print(my_string) # Output "Hello World"

Mutable and immutable types also differ in how they are stored in memory and used, which affects how the object performs and is used. When writing code, consider using mutable or immutable types based on your specific needs and performance.

Guess you like

Origin blog.csdn.net/cnds123/article/details/133136144