3 ms·
> If cleanup needs to happen, the Pythonic way is to make the lifetime of a resource explicit with a context manager. in some cases resources are long-lived an
by uryga 8y ago
> If cleanup needs to happen, the Pythonic way is to make the lifetime of a resource explicit with a context manager.
in some cases resources are long-lived and not possible to enclose in a context manager. as an example, a while ago i wrote some PyOpenGL cide and wanted to automatically release some temp GPU buffers when no longer needed. however, they had to live for multiple iterations of the app's main loop, so a context manager wouldn't work – __del__ was the best place to release them.
- uryga 8y agoto be fair though, __del__ did cause problems. when the app was exiting, PyOpenGL would release the GL context before my buffers were __del__'ed, so the __del__s tried to free a buffer that stopped existing and crashed. it'd be nice if those methods were called at a predictable time and not "sometime after the object's RC drops to zero, maybe".