Python 中可變物件和不可變物件


在本文中,我們將向您解釋 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、凍結集** 和 **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

可變與不可變

可變 不可變
物件的狀態一旦建立就可以更改 物件的狀態一旦建立就不能更改
可變物件不是執行緒安全的 不可變物件是執行緒安全的
由於可變物件不是最終的,因此程式設計師可以在繼續使用相同物件的同時不斷更改它們。 在建立不可變物件之前,必須將類設為 final。

結論

在本節中,我們學習了可變物件和不可變物件,以及可變物件和不可變物件的示例及其原始碼。我們還學習了可變物件和不可變物件之間的區別。

更新於: 2022年9月15日

559 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.