【发布时间】:2011-06-24 22:31:14
【问题描述】:
我有一个如下所示的课程:
class A:
def __init__(self, filename, sources):
# gather info from file
# info is updated during lifetime of the object
def close(self):
# save info back to file
现在,这是在一个服务器程序中,因此它可能会在没有事先通知的情况下被信号关闭。如果可能的话,定义它以确保类保存它的信息是否安全?
def __del__(self):
self.close()
如果不是,您会建议什么作为解决方案?
【问题讨论】:
-
不要使用 'del' 来处理这种事情。它不能保证在你的进程运行的生命周期内被调用,甚至在退出时也不被调用。
标签: python del resource-cleanup