在 Python 中命名可变和不可变对象

pythonserver side programmingprogramming更新于 2025/4/29 3:56:17

在本文中,我们将向您解释 Python 中的可变和不可变对象。

Python 将一切视为对象。当我们实例化一个对象时,会为其分配一个唯一的 ID。我们无法修改对象的类型,但可以更改其值。例如,如果我们将变量 a 设置为列表,则无法将其更改为元组/字典,但可以修改该列表中的条目。

在 Python 中,有两种对象。一方面,有些对象可以改变其内部状态(对象内部的数据/内容),即可以使用预定义的函数或方法进行更改,另一方面,有些对象无法改变其内部状态(对象内部的数据/内容)。

在 Python 中,有两种类型的对象 -

  • 可变对象

  • 不可变对象。

总结一下区别,可变对象可以改变其状态或内容,但不可变对象不能。

可变对象

可变对象是那些在我们实例化它们之后可以修改的对象。我们可以使用各种方法和函数来改变可变对象。当使用某些方法和函数时,原始对象会被修改。

在对可变对象进行修改时,存储这些对象的内存保持不变。

Python 中可变对象的示例有

  • 列表

  • 字典

  • 集合

列表

列表是可变的,这意味着我们可以使用赋值或索引运算符更改列表中的元素。

示例

# input list inputList = ['hello', 'tutorialspoint', 'python', 1] # printing the original input list print("The original input list:", inputList) # modifying the list element at index 2 using indexing inputList[2]= 'welcome' # printing input list after modifying print("Input list after modification:", inputList)

输出

执行时,上述程序将生成以下输出 -

The original input list: ['hello', 'tutorialspoint', 'python', 1]
Input list after modification: ['hello', 'tutorialspoint', 'welcome', 1]

我们获取一个包含一些随机值的列表,然后用另一个随机值更改列表的一个元素;因为列表是可变的数据类型,所以元素的值被更改而不会引发任何错误。

字典

由于字典是可变的,我们可以使用内置函数 update() 来更新它们。

示例

# input list inputList = ['hello', 'tutorialspoint', 'python', 1] # printing the original input list print("The original input list:", inputList) # modifying the element at index 2 using indexing inputList[2]= 'welcome' # printing input list after modifying print("Input list after modification:", inputList)

输出

执行时,上述程序将生成以下输出 -

The original input list: ['hello', 'tutorialspoint', 'python', 1]
Input list after modification: ['hello', 'tutorialspoint', 'welcome', 1]

不可变对象

这些是内置类型,例如 int、float、bool、string、unicode、Frozen Set 和 tuple。换句话说,不可变对象一旦创建就无法修改。

当您对不可变对象进行更改时,初始化期间存储它们的内存会更新。

int

由于 int 数据类型是不可变的,因此我们无法修改或更新它。

如前所述,不可变对象在更新时会更改其内存地址。

示例

# input number inputNumber = 5 # printing the memory address of the int variable print('Before updating, memory address = ', id(inputNumber)) # here we are updating an int object by assigning a new value to it. # changing the same input number variable by assigning a new value to it inputNumber = 10 # printing the memory address of the int variable after updating using id() function print('After updating, memory address = ', id(inputNumber))

输出

执行时,上述程序将生成以下输出

Before updating, memory address =  140509421819936
After updating, memory address =  140509421820096

类似地,float 数据类型在更新后也会改变其内存地址值。

tuple

元组同样是不可变的,这意味着我们不能在其中添加或更新任何内容。在修改元组中的任何项时,我们会收到错误"tuple"对象不支持项分配。

示例

# input tuple inputTuple = ('hello', 'tutorialspoint', 'python') # printing the original input tuple print("The original input tuple:", inputTuple) # modifying the tuple element at index 2 using indexing inputTuple[2]= 'welcome' # printing input tuple after modifying print("Input tuple after modification:", inputTuple)

输出

The original input tuple: ('hello', 'tutorialspoint', 'python')
Traceback (most recent call last):
File "main.py", line 6, in <module>
inputTuple[2]= 'welcome'
TypeError: 'tuple' object does not support item assignment

我们取一个包含一些随机值的元组,然后用另一个随机值更改列表的元素,因为元组是不可变的数据类型,元素的值不会改变,并且会引发异常。

字符串

由于字符串是不可变的,因此我们无法在其中添加或更新任何内容。在修改字符串的任何部分时,我们遇到了问题,表明字符串本质上是不可变的。

示例

# input string inputString = 'Welcome to tutorialspoint python' # changing the string character at index 1 inputString[1] = 'a' print(inputString)

输出

Traceback (most recent call last):
File "main.py", line 4, in <module>
inputString[1] = 'a'
TypeError: 'str' object does not support item assignment

可变与不可变

可变 不可变
对象状态一旦创建即可更改 对象状态一旦创建则无法更改
可变对象不是线程安全的 不可变对象是线程安全的
由于可变对象不是最终的,程序员可以在使用相同对象的同时继续更改它们。 在创建不可变对象之前,将类设为最终的非常重要。

结论

我们在本节中了解了可变和不可变对象,以及可变和不可变对象的示例及其源代码。我们还了解了可变和不可变对象之间的区别。


相关文章


有用资源