Feb-23-2019, 03:10 AM
(Feb-23-2019, 02:57 AM)ichabod801 Wrote: __del__ is triggered when an instance is garbage collected, not the class. So I can't imagine calling it to shut down class attributes. Then those attributes would not be available for later instances.
I used class variables for two things: constants specific to the class and used across instances, and data about the instances (like a counter for how many have been created, to make unique IDs for the instances). I can't say that's all I would use them for, but that is all I've ever felt the need to use them for.
In Python, classes are also objects, and if they are not referenced, they are destroyed and all their variables as well.
As for the role of class variables: I see convenience in them as a selection of similarities for the whole class. For example, for a class of cars 4 wheels and the presence of a motor can be set as class variables, and all other characteristics (color, upholstery, number of seats ...) are characteristics of a particular instance. This allows you to immediately see the inherent characteristics of the object.