I’m programming a complex tree structure in Python and I’m tired of creating tree objects and filling them with data everytime I run the code.
So, is there a way to make your objects “durable” in Python. With “durable” I mean if there is a way to save all my objects as bytes in a file, so when I run the code again, I can create and fill all my objects with the data in the byte-file, restoring the state I left the last time I ran the code.
I guess there may be a post with this same question, but I can’t think of a better term for “durable” in english.
You can use the
picklemodule toserializeyour data (which is the general phrase for turning your in-memory representation into something that can be written to disk). You read more in the Python documentation.The simplest code looks like this to store your object in a file:
And to read it back in: