Python Forum
How to save a class object to a file? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to save a class object to a file? (/thread-19317.html)



How to save a class object to a file? - SheeppOSU - Jun-22-2019

i was using json to save some data for my game. While I was trying to save I noticed I got an error on serializing the Player class. I could save all the stats of the player, but that would be a hassle. Is there a way I can save the Player class to a file.


RE: How to save a class object to a file? - nilamo - Jun-22-2019

Pickle or Shelve.

https://docs.python.org/3/library/pickle.html
https://docs.python.org/3/library/shelve.html

If it's just one object (the player), then Pickle. Shelve is sort of like a dictionary interface for using Pickle with a lot of different things.


RE: How to save a class object to a file? - metulburr - Jun-22-2019

It should be noted that pickling can be dangerous. If you are just using it for a local game, then you dont have to worry about anything.