-6

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?

mertyildiran
  • 6,477
  • 5
  • 32
  • 55
  • 2
    Objects are deleted only when there are no names that refer to them. In that case, your object can't be used, so why do you want it to be around? – zondo Mar 13 '16 at 22:10
  • 3
    `del` doesn't mean "force deletion". – BrenBarn Mar 13 '16 at 22:11
  • I'm afraid of accidentally delete some object from a list? What is wrong with question? – mertyildiran Mar 13 '16 at 22:13
  • @BrenBarn So what is force deletion that mentioned in this question: http://stackoverflow.com/questions/6772481/how-to-force-deletion-of-a-python-object – mertyildiran Mar 13 '16 at 22:15
  • 2
    `del` deletes a *name*, not an object. The object is deleted only if all names that point to it are removed. – zondo Mar 13 '16 at 22:16
  • 2
    @mertyildiran: That question indicates a misunderstanding of `del` and object deletion. The *answers* to the question show better ways of achieving what that question was trying to do with `del`. What are *you* trying to do with `del`? Basically, when an object gets deleted in Python is not under your control. There is no such thing as "force deletion" in Python. – BrenBarn Mar 13 '16 at 22:18
  • If your question has anything to do with deleting items in a list I think that should be explained in the question. – Stop harming Monica Mar 13 '16 at 22:19
  • @zondo I'm sorry, I didn't say `del` is an object. But I have a lack of knowledge about `del` that is obvious. – mertyildiran Mar 13 '16 at 22:19
  • 1
    What I mean is this: `x = [0]` `y = x` `del x` The list still exists. `del` just means that the *name* `x` no longer exists. The list itself exists until all names for it are removed. – zondo Mar 13 '16 at 22:21
  • @zondo Only by garbage collector right? What do you mean with names? – mertyildiran Mar 13 '16 at 22:23
  • So you mean `del` is removing pointer in C? Allocated memory block will be protected even the name doesn't exist anymore. So it's not completely deleting it. – mertyildiran Mar 13 '16 at 22:26
  • 3
    @mertyildiran This is not C, you do not allocate memory blocks, instead you create objects and give names to them. – Stop harming Monica Mar 13 '16 at 22:28
  • @Goyo So what's the condition for deletion of an object? Could you give me an example? "until all names for it are removed" what is the meaning of this? – mertyildiran Mar 13 '16 at 22:33

1 Answers1

3

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)
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • I've just realized that I need to use an immutable object like **tuple** to store names that are referencing to my "protected" object. Please add this to your answer and I will make it correct answer. – mertyildiran Mar 13 '16 at 23:04
  • @mertyildiran Added, but keep in mind converting to tuple will not allow any modification to the tuple itself (It will however allow modification to the inner objects) – Bharel Mar 13 '16 at 23:13