I Need Clarification Here

Very simple question…but I’m not sure of the answer.

Lets say you defined a python class and gave it some attributes and methods (functions).

class Girl87():
   def __init__(self):
      self.Gbreats = 72
      self.Gwaist = 12
      self.Garse = 55

   def destroy(self):
      del self.Gbreats
      del self.Gwaist
      del self.Garse

DreamGirl = Girl87()

Now, lets say you want to trash this instance, so you type:

DreamGirl.destroy()
del DreamGirl

I already know the “destroy” call would in fact delete the attributes of the instance (I would imagine panda would free up any resources that were stored in any deleted attribute)

but,

What about the method (function) called “destroy?” Which brings me to my question…

Does python destroy all methods (functions) that belonged to an instance if that instance itself is destroyed?

You can’t destroy the methods like the attributes. I tested this with python’s IDLE program; so I’m guessing the methods go up in smokes along with the instance, once the instance destroyed

:question:

The methods belong to the class, not to the instance. That is, all of the method objects exist from the time you import the .py file, and they continue to exist until the program exits. Creating new instances of the object does not create new methods, and destroying instances does not destroy them. The same method objects are shared by all instances. So there’s no need to attempt to clean them up when the instance goes away.

Actually, there’s not really a need to so aggressively clean up all of your instance data, either. Normally, when your last reference to an instance goes away, the instance itself is destructed automatically; and when this happens, all of the instance data is also destructed. So in your example, there is no need to explicitly call destroy(), because everything it does will be done automatically anyway.

The only exception is when you have circular references, for instance if you had a member like:

self.backPointer = self

which would be a silly thing to do, but it would prevent your instance from being cleaned up properly. (Except that the garbage collector would eventually get it, but that’s another topic.)

For the most part, Python is designed so you don’t ever have to think about explicitly cleaning up objects. This ideal is disrupted a little bit when you get Panda in the mix, and so Panda does provide a few destroy() methods where appropriate, for instance on DirectGui objects. But these, too, are exceptions to the rule.

David