在python 2.x中,我应该调用object .__ del__吗? [英] In python 2.x should I call object.__del__?

查看:83
本文介绍了在python 2.x中,我应该调用object .__ del__吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3.x中,所有类都是 object 的子类。在2.x中,您必须明确声明 class MyClass(object)。而且,由于我尝试编写尽可能多的3.x兼容代码,所以我将 object 子类化。

In Python 3.x all classes are subclasses of object. In 2.x you have to explicitly state class MyClass(object). And, as I'm trying to write as much 3.x compatible code as possible, I'm subclassing object.

在我的程序中,我使用的是 __ del __ 方法,我想知道是否应该调用 object .__ del __(self),还是神奇地解决了这个问题?

In my program, I'm using the __del__ method, and I wanted to know if I should be calling object.__del__(self), or if that's magically taken care of?

感谢,
Wayne

Thanks, Wayne

编辑:
看来我的意思有些混乱-在声明的文档中:

It appears there is some confusion what I mean - in the documents it states:

如果基类具有 __ del __()方法,则派生类的 __ del __()方法(如果有的话)必须显式调用它,以确保正确删除实例的基类部分。

If a base class has a __del__() method, the derived class’s __del__() method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance.

所以我想要知道我是否需要:

So I wanted to know if I needed:

def __del__(self):
    object.__del__(self)

或其他合适的选择。

推荐答案

好吧,对象实际上没有 __ del __ 方法,所以不,您没有

Well, object doesn't actually have a __del__ method, so no, you don't need to call it.

>>> object().__del__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute '__del__'

这篇关于在python 2.x中,我应该调用object .__ del__吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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