Python Forum

Full Version: What is the strategy for working with class variables?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Class variables contain variables common to all objects of the class and thus show the inalienable properties of the objects. But unfortunately, they are not collected by the garbage collector, because they belong to the whole class, which in turn belongs to a module, which are very rarely unloaded. Therefore, I see three strategies for their use:
1. In class variables, store the necessary user objects, and for all objects that need to call the destructor explicitly
class MyClass:
    WRITER = writers.DbWriter()
    def __del__(self):
        self.WRITER.close()
2. Class variables store auxiliary data that, on the one hand, allows you to see common features of the class, and on the other hand, they are easily converted to real objects during initialization in the init() method
class MyClass:
    WRITER = 'DbWriter'
    def __init__(self):
        if WRITER == 'DbWriter':
            self.writer = writers.DbWriter()
3. Discard class variables because no garbage collector is called for them.
class MyClass:
    def __init__(self):
        self.writer = writers.DbWriter()
What is the best strategy to use?
__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.
(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.
(Feb-23-2019, 11:02 AM)sritaa Wrote: [ -> ]implement

Thanks, but my question is not how to use class variables, but is it worth using them.