I have some test.py file:
class A: def __init__(self): print("A init") def __del__(self): print("A del") a = A() When I run it 10 times (python3 test.py) it always produces next output:
A init A del But if I add sys.exit call to end of script:
import sys class A: def __init__(self): print("A init") def __del__(self): print("A del") a = A() sys.exit(-1) in 5 of 10 cases (randomly) i have
A init and in second half of cases:
A init A del I use Python3.4.3 [MSC v.1600 32 bit] on Windows 7 x64. So why __del__ method called not every time? Do I need to use some other exit method to pass return code of script and have all destructors guaranteed executed? And one more related question: is it possible to execute destructors on receive SIGTERM or SIGKILL from OS?
__del__. You can catch signals with thesignalmodule, except for SIGKILL, which can't be caught.sys.exit(0);, which means that the code worked properly.__del__. May be better decision to separate deinit in some other method and completely rely on users of wrapper?