多次被称为驱动器

发布于 2025-02-10 08:44:30 字数 532 浏览 2 评论 0原文

我正在尝试检查 new init del 的作品,并在下面提到的代码下运行,引起我注意的是destructor被称为两次而且我不知道它的确切原因,有人可以帮忙吗?

class Example:

    # creation of new instance
    def __new__(cls):
        print("example created", object.__new__(cls))
        return object.__new__(cls)

    # Initializing
    def __init__(self):
        print("Example Instance.")

    # Calling destructor
    def __del__(self):
        print("Destructor called, Example deleted.")

obj = Example()
del obj

i am trying to check how new , init and del works and was running below mentioned code , what caught my attention is destructor is being called twice and i don't know the exact reason of it, Can anyone help with it?

class Example:

    # creation of new instance
    def __new__(cls):
        print("example created", object.__new__(cls))
        return object.__new__(cls)

    # Initializing
    def __init__(self):
        print("Example Instance.")

    # Calling destructor
    def __del__(self):
        print("Destructor called, Example deleted.")

obj = Example()
del obj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2025-02-17 08:44:30

来自Geeksforgeeks:

__DEL __()方法是Python中称为击ol方法的一种。当对象收集垃圾时,对对象的所有引用均已删除时,称为。注意:当对象退出参考或程序结束时,也会删除对对象的引用。

Python使用参考计数和垃圾收集器自动为您管理内存。也就是说,不必要的参考文献被自动删除。

因此,您需要明确创建它以避免这种行为。

class Example:
   instance = ""

    # creation of new instance
    def __new__(cls):
        instance = object.__new__(cls)
        print("example created", instance)
        return instance

    # Initializing
    def __init__(self):
        print("Example Instance.")

    # Calling destructor
    def __del__(self):
        print("Destructor called, Example deleted.")

结果:

>>> obj = Example()
example created <__main__.Example object at 0x7fd0b3efa5b0>
Example Instance.
>>> del obj
Destructor called, Example deleted.
>>>

From geeksforgeeks:

The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected. Note : A reference to objects is also deleted when the object goes out of reference or when the program ends.

Python automatically manages memory for you using reference counting and garbage collector. That is, references unnecessary are being automatically deleted.

So you need to create it explicitly to avoid this behavior.

class Example:
   instance = ""

    # creation of new instance
    def __new__(cls):
        instance = object.__new__(cls)
        print("example created", instance)
        return instance

    # Initializing
    def __init__(self):
        print("Example Instance.")

    # Calling destructor
    def __del__(self):
        print("Destructor called, Example deleted.")

result:

>>> obj = Example()
example created <__main__.Example object at 0x7fd0b3efa5b0>
Example Instance.
>>> del obj
Destructor called, Example deleted.
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文