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
  Open/save file on Android frohr 0 265 Jan-24-2024, 06:28 PM
Last Post: frohr
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  how to save to multiple locations during save cubangt 1 497 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,089 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 2,755 Feb-20-2023, 07:19 PM
Last Post: avd88
  Save multiple Parts of Bytearray to File ? lastyle 1 898 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
Sad Want to Save Print output in csv file Rasedul 5 10,597 Jan-11-2022, 07:04 PM
Last Post: snippsat
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,768 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  How to save Matplot chart to temp file? Morkus 2 4,416 Jun-12-2021, 10:52 AM
Last Post: Morkus
  How to save modification of a file in original file not a new one? mjrezayani 3 2,974 Jun-04-2021, 01:28 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