Python 可变和不可变对象
在这篇文章中,我们将解释Python中的可变和不可变对象。
Python将所有东西都视为对象。当我们实例化一个对象时,给它分配一个唯一的标识符。我们不能修改对象的类型,但是我们可以修改其值。例如,如果我们将变量a设置为列表,那么我们不能将其更改为元组/字典,但是我们可以修改列表中的条目。
在Python中,有两种类型的对象。一方面,存在可以更改其内部状态(对象内部的数据/内容)的对象,即可以使用预定义的函数或方法进行更改,另一方面,存在无法更改其内部状态的对象(对象内部的数据/内容)。
在Python中,有2种类型的对象:
- 可变对象
-
不可变对象
为了总结差异, 可变 对象可以更改其状态或内容,而 不可变 对象不能。
可变对象
可变对象是我们实例化后可以修改的对象。我们可以使用各种方法和函数来修改可变对象。当使用某些方法和函数时,原始对象会被修改。
在对可变对象进行修改时,存储这些对象的内存保持不变。
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”对象不支持项目分配。
示例
# 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
可变 vs 不可变
| 可变的 | 不可变的 |
|---|---|
| 对象的状态在创建后可以改变 | 对象的状态在创建后无法改变 |
| 可变对象不是线程安全的 | 不可变对象是线程安全的 |
| 由于可变对象不是final的,程序员可以在使用相同的对象时不断地改变它们 | 在创建不可变对象之前,将其类声明为final是很重要的 |
结论
在本节中,我们学习了可变和不可变对象,以及可变和不可变对象的示例及其源代码。我们还学习了可变和不可变对象之间的区别。
极客笔记