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?
#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


Messages In This Thread
RE: What is the strategy for working with class variables? - by AlekseyPython - Feb-23-2019, 03:10 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