Python 中的析構器

用戶調用析構器來銷毀對象。在 Python 中,開發人員可能不像在 C++語言中那樣需要析構器。這是因為 Python 有一個垃圾收集器,它的功能是自動處理內存管理。

在本文中,我們將討論 Python 中的析構器是如何工作的,以及用戶何時可以使用它們。

del() 函數在 Python 中用作析構器。當對象的所有引用都被刪除,變成垃圾收集時,用戶可以調用 del() 函數。

語法:


def __del__(self):
    # the body of destructor will be written here. 

用戶還應該注意,當對象超出引用或代碼結束時,對對象的引用也會被刪除。

在下面的例子中,我們將使用 del()函數和 del 關鍵字刪除對象的所有引用,這樣析構器將自動涉及。

例如:


# we will illustrate destructor function in Python program
# we will create Class named Animals
class Animals:

    #  we will initialize the class
    def __init__(self):
        print('The class called Animals is CREATED.')

    # now, we will Call the destructor
    def __del__(self):
        print('The destructor is called for deleting the Animals.')

object = Animals()
del object 

輸出:

The class called Animals is CREATED.
The destructor is called for deleting the Animals.

解釋-

在上面的代碼中,當對象的引用被刪除或者程序結束後,析構器已經被調用。這意味着對象的引用計數變為零,而不是當對象超出範圍時。我們將通過展示下一個例子來解釋這一點。

我們還可以注意到,析構器是在程序結束後調用的。

示例:


# We will create Class named Animals
class Animals:

    #  Initialize the class
    def __init__(self):
        print('The class called Animals is CREATED.')

    # now, we will Call the destructor
    def __del__(self):
        print('The destructor is called for deleting the Animals.')

def Create_object():
    print('we are creating the object')
    object = Animals()
    print('we are ending the function here')
    return object

print('we are calling the Create_object() function now')
object = Create_object()
print('The Program is ending here')

輸出:

we are calling the Create_object() function now
we are creating the object
The class called Animals is CREATED.
we are ending the function here
The Program is ending here
The destructor is called for deleting the Animals.

現在,在下一個示例中,我們將看到,當調用函數()時,它將創建類 Zebra 的實例,該實例將自身傳遞給類 Lion,然後類 Lion 將設置對類 Zebra 的引用,這將導致循環引用。

示例:


class Animals:

    #  we will initialize the class
    def __init__(self):
        print(' The class called Animals is CREATED.')
class Lion:
    def __init__(self, zebraa):
        self.zebra = zebraa
class Zebra:
    def __init__(self):
        self.lion = Lion(self)
    def __del__(self):
        print("Zebra is dead")
def function():
    zebra = Zebra()

function()

輸出:

Zebra is dead

一般來說,Python 的垃圾收集器(用於檢測這些類型的循環引用)也會移除引用。但是,在上面的例子中,自定義析構器被用來將這個項目標記為無法收集。

用簡單的語言來說,它意味着垃圾收集器不知道對象應該被銷毀的順序,所以它離開了它們。因此,如果用戶的實例包含在這個循環引用中,只要應用運行,它們就會一直存儲在內存中。

結論

在本文中,我們解釋了 Python 中析構器的功能,以及用戶如何使用它們來刪除那些引用已經從內存中移除的對象。


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/240256.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:21
下一篇 2024-12-12 12:21

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29

發表回復

登錄後才能評論