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


Messages In This Thread
What is the strategy for working with class variables? - by AlekseyPython - Feb-23-2019, 02:34 AM

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