python的pickle模块可以保存和读取对象,实例代码如下:

import pickle

filename = "tmp.pkl"

class Object:
    char = "a"
    def __init__(self):
        self.char = "b"

    def change(self):
        self.char = "c"

object = Object()
print(object.char)

file_output = open(filename, "wb")
pickle.dump(object, file_output)
file_output.close()

file_input = open(filename, "rb")
read_object = pickle.load(file_input)
file_input.close()

print(read_object.char)
read_object.change()
print(read_object.char)