Mar-16-2020, 11:53 AM
You can standard pickle module, e.g.
import pickle from io import BytesIO bs = BytesIO() class MyClass: ... mm = MyClass() # lets store mm as a bytestring pickle.dump(mm, bs) bs.seek(0) # move to the begining of the File-like object print(bs.read()) # this bytesting represents the instance of MyClass (mm) # you can restore the object, e.g. bb.seek(0) restored = pickle.load(bs)If objects are really complex, you could try to customize
__setstate__
and __getstate__
methods (see official docs on pickle module for details).