Python Forum
What is the strategy for working with class variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the strategy for working with class variables?
#1
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?
Reply
#2
__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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unchangeable variables in a class? Calab 12 1,404 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Moving average strategy irina_shubina 2 1,745 Jul-31-2022, 05:11 PM
Last Post: paulyan
  strategy to troubleshoot what pyinstaller misses hammer 0 912 May-23-2022, 01:05 AM
Last Post: hammer
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Best strategy for creating .exe for windows hammer 4 1,453 Apr-05-2022, 12:47 AM
Last Post: hammer
  Strategy on updating edits back to data table and object variables hammer 0 1,163 Dec-11-2021, 02:58 PM
Last Post: hammer
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,779 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 10,382 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,866 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  calculate daily return in percent in forex as to some strategy? alen 1 2,181 Mar-12-2021, 10:03 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020