I have a script where I create an object over a cloud using API and want to delete it over some occasions, at the end of the script.
class ObjCreator():
def __init__(self, keep_object: bool):
self.keep_object = keep_object
print("init")
def create_object(self):
print("Created object using API")
def delete_object(self):
print("Deleting object using API")
def __del__(self):
print("Deleting object using API only if keep_object is False")
if not self.keep_object:
self.delete_object()
I've seen some memory leaks over Python programs, so obviously the GC isn't prefect (i.e., uncollectable objects).
My fear is that overriding the del function would cause a memory leak. Is that possible? If so, do you have any suggestion to a different, elegant solution?