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/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

发表回复

登录后才能评论