Cleaning up a custom class [SOLVED]

I have a custom class that looks like the following, plus there are some functions I didn’t include because I don’t they are relevant to my question.

class KidMover:
# This class is used to control the movement of kids through the school. Each mover can be assigned a number of kids to 
# move.

	def __init__(self, kid, dotList, endDot):
		self.nodePath = render.attachNewNode("KidMover")
		# Creates a nodePath to the mover, which allows the mover to have functions like set position, etc.
		self.kidList = []
		# A list to hold the kids assigned to the mover.
		# Add the kid to the kidList.
		self.pathList = []
		self.nextDot = 0
		self.generatePath(kid.getPos(), endDot, dotList)

I want to be able to completely destroy and remove from memory an instance of this class. How would I go about doing that?

Python instances are destroyed and removed when the last reference to them is removed. So the way to destroy this instance is the same as for any other instances: you need to delete all references to the instance.

So, if you create an instance with:
self.kidMover = KidMover(kid, dotList, endDot)

You would destroy this instance with:
self.kidMover = None

which replaces the value of self.kidMover with None, discarding the previous reference to the KidMover instance you had stored there. Assuming you have no other references to the same object, that will destroy the object.

That has nothing to do with the code within the class itself (other than destroying any circular references created within the class, but I don’t see you doing that here).

Note that destroying the instance has nothing to do with destroying any side-effects of the instance’s constructor (like the NodePath your instance creates). You’ll have to clean that up separately, perhaps with a cleanup() method in your class.


That’s simple enough, thanks David.