Python Forum

Full Version: How to save a class object to a file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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.