什么是__del__方法,如何调用? [英] What is the __del__ method, How to call it?

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

问题描述

我正在阅读代码.在其中定义了__del__方法的类.我发现此方法用于销毁该类的实例.但是,我找不到使用此方法的地方.主要原因是我不知道如何使用此方法,可能不是这样:obj1.del().所以,我的问题是如何调用__del__方法?

I am reading a code. There is a class in which __del__ method is defined. I figured out that this method is used to destroy an instance of the class. However, I cannot find a place where this method is used. The main reason for that is that I do not know how this method is used, probably not like that: obj1.del(). So, my questions is how to call the __del__ method?

推荐答案

__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__()中-相反,该代码属于tryfinally子句with语句中的块或上下文管理器.但是,__del__有效用例:例如如果对象X引用Y,并且还在全局cache(cache['X -> Y'] = 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 you do so only if you know that it doesn't mind to 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天全站免登陆