Search code examples
pythondata-persistence

Updating static variables of a class in python permanently


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.


Solution

  • 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.