Objects in python have reference count which basically means the amount of places an object exist in. Using del
removes one reference to the object (it does not force delete it). __del__
is then called when 0 references are left. You may create a new reference to the object and this way prevent it’s deletion like so:
class obj:
def __del__(self):
global _ref
_ref = self
return
This will prevent the deletion of the object by creating a global reference.
Keep in mind it is not suggested to do so as it will prevent the garbage collector from working correctly.
UPDATE:
In case you are using a list, you should convert it to an immutable object such as a tuple
in order to prevent changes like deletion as so:
mylist = [obj1, obj2, obj3, obj4 ....]
mylist = tuple(mylist)
2
solved How to make an object indestructible even with force deletion?