How to write global class variables in a file and retrieve later

75 views Asked by At

consider a class somewhat like the following:

class person:
   personCount = 0
   def __init__(self, name):
      self.name = name
      personCount = perosnCount+1

I want to be able to store all class variables in a file and retrieve them en-block when the program is run again at a later point in time, so that we can correctly maintain class variables like personCount.

Of course, one could save one by one and restore later when the program is run again. But that seems too untidy.

Basically I am looking for a method that does not depend on particular class variables of the specific class in question.

1

There are 1 answers

0
tdelaney On

That's what the pickle module is for. It saves class data to a file and rebuilds it later on future runs.