Is __del__ really a destructor?

前端 未结 4 737
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 14:58

I do things mostly in C++, where the destructor method is really meant for destruction of an acquired resource. Recently I started with python (which is really a fun and fantast

4条回答
  •  情书的邮戳
    2021-02-05 15:12

    So uncommon it is that I have learned about it today (and I'm long ago into python).

    Memory is deallocated, files closed, ... by the GC. But you could need to perform some task with effects outside of the class.

    My use case is about implementing some sort of RAII regarding some temporal directories. I'd like it to be removed no matter what.

    Instead of removing it after the processing (which, after some change, was no longer run) I've moved it to the __del__ method, and it works as expected.

    This is a very specific case, where we don't really care about when the method is called, as long as it's called before leaving the program. So, use with care.

提交回复
热议问题