Python Forum
How to save a class object to a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save a class object to a file?
#1
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.
Reply
#2
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.
Reply
#3
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what is solution to save and reload a object? eue 4 1,910 Nov-23-2024, 02:15 PM
Last Post: DeaD_EyE
  Read TXT file in Pandas and save to Parquet zinho 2 1,245 Sep-15-2024, 06:14 PM
Last Post: zinho
  Printing out incidence values for Class Object SquderDragon 3 1,234 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  Open/save file on Android frohr 0 1,112 Jan-24-2024, 06:28 PM
Last Post: frohr
  error in class: TypeError: 'str' object is not callable akbarza 2 1,706 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  how to save to multiple locations during save cubangt 1 1,285 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 2,666 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 6,445 Feb-20-2023, 07:19 PM
Last Post: avd88
  Save multiple Parts of Bytearray to File ? lastyle 1 1,583 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
Sad Want to Save Print output in csv file Rasedul 5 15,434 Jan-11-2022, 07:04 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020