Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

如何在__del__()中执行复杂的操作?

我想实现一个功能,在一个类销毁时把一些数据记录到excel,我用的方法是在__del__()里使用openpyxl。但当程序终止时自动执行的__del__()会触发异常。应该是python退出时进入了某种状态导致的,不同用法触发不同错误。如果用新建xlsx方式,在save时会触发ImportError: sys.meta_path is None, Python is likely shutting down异常。

因为对象何时关闭是不可预知的,我只想到__del__()的方法。请问有什么办法实现我想要的功能?谢谢。
image
image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以参考下官方文档https://docs.python.org/3/reference/datamodel.html#object.__del__
对于你说的这种场景,是否有具有的实例可以完善下,以方便参考


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...