We're starting with the Explosion classes because they don't create instances of any other classes. A depth-up kind of strategy is as good as any for this kind of testing.

  1. Open the ExplosionClasses_00.py file from the Chapter12 folder.
  2. Add the following method to the bottom of the Pop class:
    def __del__(self):
    print("Pop Removed")
    
  3. Add this method to the bottom of the Boom class:
    def __del__(self):
    print("Boom Removed")
    
  4. Resave the file with the same name and run the game. Watch the command prompt for our prints when Pops and Booms vanish.
  5. We don't see the prints! That's because we have two errors in our classes that need to be fixed. Firstly, we're using a Sequence to call our destroy method after a set amount of time, but we aren't starting that Sequence so our destroy method is never called! Add this line of code to the bottom of the __init__ method for both classes:
    self.seq.start()
    
  6. The second error is a little more subtle. Our Sequence is constructed with a Func Interval, and we give one of our class methods to that Func Interval. That means the Sequence is storing a reference to the method in the Func Interval, and that reference is enough to keep the class instance alive. Add this line to both destroy methods, right above the line that says self.self = None:
    self.seq = None
    
  7. Resave the file as ExplosionClasses_01.py. Open GunClasses_00.py and update its import to use ExplosionClasses_01.py, and then resave it with the same name.
  8. Run the game again and watch the command prompt. This time we get our printouts telling us that the Pops and Booms have been removed from memory.
  9. Remove both of the __del__ methods from the classes and resave the file as ExplosionClasses_02.py.
Time for action - collecting garbage from the Explosion classes