How can I make an object such indestructible that even with using del
reserved word (I think it means force deletion) it won't be deleted?
I'm storing the objects in a list, like this:
list = [obj1, obj2, obj3, obj4 ....]
del list[0]
I think I have to do some tricks with __del__()
within the class of object but how?
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)