我创建了一个这样的实例:
class some_class():
def __init__(self, aa, bb):
self.a = aa
self.b = bb
def add(self):
self.c = self.a + self.b
return self.c
instance001 = some_class(2,200)
现在我尝试将 instance001
保存到硬盘以供将来使用。
with open('Storage', 'w') as file:
file.write(instance001)
这行不通。如何存储实例?
首选格式是 hdf,但欢迎任何其他想法。
注意:Pandas 被大量使用。
最佳答案
如果是纯 Python 类,您可以简单地使用 pickle
:
import pickle
with open('Storage', 'wb') as f:
pickle.dump(instance001, f)
并加载它:
with open('Storage', 'rb') as f:
instance002 = pickle.load(f)
print(instance002.a) # 2
print(instance002.b) # 200
关于python - 如何将实例对象保存到硬盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118554/