Reputation: 32221
I am new to Python, I want to understand more the purpose of the __delete__
method.
In my case I got a class that opens a file and I want to add file.close
in __delete__
method. Would you consider this as a good idea? Or does the __delete__
is used in a different way?
From the docs I am not sure that am using it right. http://python-reference.readthedocs.io/en/latest/docs/dunderdsc/delete.html
Upvotes: 0
Views: 100
Reputation: 40884
The rule of thumb: if you want to use a destructor for resource management, don't. It's unpredictable and unreliable.
Your options:
contextlib
) to deterministically free resources when a scope ends.Upvotes: 2