3

Hi I have a class like the following:

class Point:
   def __init( self, x=0, y=0):
      self.x = x
      self.y = y
   def __del__(self):
      pass #do nothing

I didn't specify any deleting operation in __del__ overriding and I didn't call any kind of super() to call "original" class method.

However, if I del an instance of Point it works anyway...how is it possible?

Edit for duplication issue: I think that this question is slightly different from:

since its point is to investigate on how it is possible that __del__ makes things not written in the overridden function, without calling another original __del__ implementation but only overriding the original function with just a simple pass. I think it looks quite strange in Python logic.

Community
  • 1
  • 1
floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • `__del__` is not a destructor. Most classes don't even have a `__del__` and don't need one. – user2357112 Jun 23 '16 at 16:54
  • in my opinion, if you don't understand, do not use __del__ it may cause you memory leak in the future. – galaxyan Jun 23 '16 at 17:01
  • @user2357112 `__del__` definitely is a destructor. However it might be worth noting that calling `del` does not actually invoke the `__del__` method, but instead decrements the object reference counter, and `__del__` will be called when that reference counter reaches 0. See more info here: [Python Data model](https://docs.python.org/2/reference/datamodel.html) – Andrew Jun 23 '16 at 17:01
  • What do you mean by "it works anyway"? From the looks of it you're just creating a memory leak... – Andrew Jun 23 '16 at 17:04
  • From the [documentation for `__del__`](https://docs.python.org/3.5/reference/datamodel.html#object.__del__): "It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits." – Matthias Jun 23 '16 at 17:04
  • @Andrew I mean that if I del an object, then there is no more reference to that object. – floatingpurr Jun 23 '16 at 21:59
  • @galaxyan Ok i will not use. But its behaviour looks very strange and I do not understand why. – floatingpurr Jun 23 '16 at 22:01
  • @superciccio14 if two objects with del have cycle refinance, python does't know which one delete first, so those two objects will be kept – galaxyan Jun 23 '16 at 22:10
  • @galaxyan ok. But it looks strange that del can delete references even if I override it with only a pass clause. – floatingpurr Jun 23 '16 at 23:05
  • 1
    @superciccio14 i believe so. if there are more than one object in the cycle has a del, the order in which the objects need to be finalized and then garbage collected cannot be determined, so the garbage collector plays it safe and keeps the objects. – galaxyan Jun 23 '16 at 23:13
  • @galaxyan ok, but I still do not understand form the overriding point of view. I defined the method. I coded inside it only a pass. I do not call anything inside dal. But del does more than a simple pass! – floatingpurr Jun 23 '16 at 23:32
  • 1
    @superciccio14 i think it related to Python reference count – galaxyan Jun 23 '16 at 23:37
  • 1
    @superciccio14 I think that when you call `del` it automatically decrements the reference counter despite the code in your `__del__` function (or lack thereof). Since it only had one reference, it goes to 0, and is still garbage collected by Python. If this is how it works then the code in the `__del__` method is most likely for doing other more specific things like initialixing the memory to 0 and whatnot. Not 100% sure but thats what I think might be happening. – Andrew Jun 24 '16 at 12:14
  • I found this topic http://stackoverflow.com/questions/1788312/why-do-constructors-not-return-values. Quite different, but I think that the reason is the same – floatingpurr Jul 04 '16 at 11:59

0 Answers0