16.8. Object lifecycle¶
In the previous examples, we define a class (template), use that class to create an instance of that class (object), and then use the instance. When the program finishes, all of the variables are discarded. Usually, we don’t think much about the creation and destruction of variables, but often as our objects become more complex, we need to take some action within the object to set things up as the object is constructed and possibly clean things up as the object is discarded.
If we want our object to be aware of these moments of construction and destruction, we add specially named methods to our object:
As Python constructs our object, it calls our
__init__ method to
give us a chance to set up some default or initial values for the object.
When Python encounters the line:
an = 42
It actually “thows our object away” so it can reuse the
to store the value
42. Just at the moment when our
an object is
being “destroyed” our destructor code (
__del__) is called. We cannot
stop our variable from being destroyed, but we can do any necessary
cleanup right before our object no longer exists.
When developing objects, it is quite common to add a constructor to an object to set up initial values for the object. It is relatively rare to need a destructor for an object.