0

How can I permanently update static variables in a class?

I found Static variables in python classes. However, this updates the variable only for the current script. When I reload my module, the variables are reset to there original initialized values.

Like suppose I want to store the number of instances of a class created by all the users till date. But whenever a user imports my class from the module, it resets the initial count to 0.

I am thinking a solution might be to store the variables in a file and every time my class is instantiated, update variables in the file.

Please suggest some better approach. Thanks for your help.

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
Govinda Totla
  • 576
  • 6
  • 19
  • 1
    You are correct that you would need to store the information permanently in a file somewhere (this could be in an external database, if you really wanted). What exactly is your question asking for? – jeremye Jun 06 '19 at 15:02
  • I was looking for some alternative method or functions if they exists. – Govinda Totla Jun 06 '19 at 15:03

1 Answers1

2

When a script stops running, all information of that script is purged from the memory unless you save it to the disk some how. Hence the use of an external file or a database. This is just a part of core software development and machine architecture.

If you are storing a single variable, a file is probably the best way to go. But a database is what you need as you start storing multiple pieces of data, types of data, and need data to relate to other pieces.

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21