根据Python (2.7) documentation :
Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead
完全和绝对忽略 __del__() 中引发的异常的最 Pythonic 方法是什么——也就是说,不仅忽略异常,而且不打印任何内容。有没有比暂时将 stderr 重定向到空设备更好的方法?
最佳答案
我假设这是在您正在编写的 __del__()
函数中,如果是这样,您自己捕获异常并忽略它。
def __del__(self):
try:
# do whatever you need to here
except Exception:
pass
stderr 的日志记录仅适用于 __del__()
中的未捕获 异常。
关于python - 忽略在 __del__() 中打印到 stderr 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977970/