According to the Python (2.7) documentation:
Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead
What would be the most Pythonic way to completely and absolutely ignore an exception raised in __del__() — that is, not only having the exception ignored but also nothing printed to sterr. Is there a better way than temporarily redirecting stderr to the null device?
I am assuming this is in a __del__()
function that you are writing, if so, just catch the exception yourself and ignore it.
def __del__(self):
try:
# do whatever you need to here
except Exception:
pass
The logging to stderr only applies for uncaught exceptions in __del__()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With