【发布时间】:2016-08-11 20:49:57
【问题描述】:
我在我定义的类中有一个__del__ 方法,用于删除通过在 ctypes 接口中调用 C++ new 创建的一些 C++ 对象。当我的类的实例被销毁时,我想删除这些对象。我有一个类的片段显示在这里:
class Graph(QtCore.QObject):
def __init__(self):
super().__init__()
#list of objects created by calls to ctypes to create pointers to C++ objects which are instantiated with C++ new
self.graphs = []
def __del__(self):
print("in delete method")
for graph in self.graphs:
# call the C++ delete to free the storage used by this graph
myctypes.graphDelete(graph)
super().__del__()
当我的 Graph 类的一个实例被删除时,__del__ 方法被调用,我看到了我的打印语句,当我在 C++ 代码的析构函数方法中设置断点时,正如预期的那样,它删除了该对象。但是,当我的__del__ 方法调用super().__del__() 时,我收到错误消息:
super().__del__()
AttributeError: 'super' object has no attribute '__del__'
如果我在子类中定义了自己的__del__方法,如何确保父类(QtCore.QObject)被删除或者父类会被自动删除?
【问题讨论】:
-
请尝试
super(Graph, self).__del__()和QtCore.QObject.__del__(self)。这应该没有什么区别,但也许它有效。 -
谢谢 Kay,我刚刚尝试了这两种方法,但仍然出现属性错误。
-
@inwhack,我相信垃圾收集器会完成它的工作,清理未使用的实例/变量
标签: python python-3.x destructor super