相关疑难解决方法(0)

如何正确清理Python对象?

class Package:
    def __init__(self):
        self.files = []

    # ...

    def __del__(self):
        for file in self.files:
            os.unlink(file)
Run Code Online (Sandbox Code Playgroud)

__del__(self)上面因AttributeError异常而失败.我理解Python__del__()调用时不保证存在"全局变量"(在此上下文中的成员数据?).如果是这种情况并且这是异常的原因,我该如何确保对象正确破坏?

python destructor

436
推荐指数
10
解决办法
40万
查看次数

标签 统计

destructor ×1

python ×1