什么是 __del__ 方法以及如何调用它? [英] What is the __del__ method and how do I call it?

查看:51
本文介绍了什么是 __del__ 方法以及如何调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个类,其中 __del__方法被定义.此方法用于销毁类的实例.但是,我找不到使用这种方法的地方.这个方法是如何使用的?像这样:obj1.del()?.

I saw a class in which a __del__ method is defined. This method is used to destroy an instance of the class. However, I cannot find a place where this method is used. How is this method used? Like that: obj1.del()?.

如何调用 __del__ 方法?

推荐答案

__del__ 是一个 终结器.当对象被垃圾收集时调用,这种情况发生在对对象的所有引用都已被删除之后的某个时刻.

__del__ is a finalizer. It is called when an object is garbage collected which happens at some point after all references to the object have been deleted.

简单情况中,这可能是在您说 del x 之后,或者,如果 x 是局部变量,则在函数结束之后.特别是,除非有循环引用,否则 CPython(标准 Python 实现)将立即进行垃圾回收.

In a simple case this could be right after you say del x or, if x is a local variable, after the function ends. In particular, unless there are circular references, CPython (the standard Python implementation) will garbage collect immediately.

然而,这是 CPython 的实现细节.Python 垃圾收集的唯一必需属性是它在所有引用被删除之后发生,因此这可能没有必要在之后发生强>根本不会发生.

However, this is an implementation detail of CPython. The only required property of Python garbage collection is that it happens after all references have been deleted, so this might not necessary happen right after and might not happen at all.

更重要的是,由于许多原因,变量可以存在很长时间,例如传播异常或模块内省可以使变量引用计数保持大于 0.此外,变量可以是引用循环的一部分——开启垃圾收集的 CPython 中断了大多数,但不是全部,这样的循环,甚至只是定期.

Even more, variables can live for a long time for many reasons, e.g. a propagating exception or module introspection can keep variable reference count greater than 0. Also, variable can be a part of cycle of references — CPython with garbage collection turned on breaks most, but not all, such cycles, and even then only periodically.

由于你不能保证它被执行,人们应该永远不要把你需要运行的代码放到__del__()中——相反,这段代码属于try 块的 >finally 子句或 with 语句中的上下文管理器.但是,__del__有效用例:例如如果一个对象 X 引用了 Y 并且还在全局 cache (cache['X -> Y'] = Y) 那么 X.__del__ 也可以删除缓存条目.

Since you have no guarantee it's executed, one should never put the code that you need to be run into __del__() — instead, this code belongs to finally clause of the try block or to a context manager in a with statement. However, there are valid use cases for __del__: e.g. if an object X references Y and also keeps a copy of Y reference in a global cache (cache['X -> Y'] = Y) then it would be polite for X.__del__ to also delete the cache entry.

如果您知道析构函数提供(违反上述指南)所需的清理,您可能希望直接调用,因为没有什么特别之处它作为一种方法:x.__del__().显然,只有在知道它可以被调用两次时才应该这样做.或者,作为最后的手段,您可以使用

If you know that the destructor provides (in violation of the above guideline) a required cleanup, you might want to call it directly, since there is nothing special about it as a method: x.__del__(). Obviously, you should only do so if you know it can be called twice. Or, as a last resort, you can redefine this method using

type(x).__del__ = my_safe_cleanup_method

这篇关于什么是 __del__ 方法以及如何调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆