Sep-13-2019, 03:39 PM
How do I write class instances to a file in binary mode? When I try to run this code,
Also, is this the best way to do what I want to do? I'm making my first app. Users will want to save some of the custom class objects that are created to their phones. I figured I'd need to have the program create a separate file to store those objects in. Is this best practice?
(Apologies if I have used any terms incorrectly or misunderstood something fundamental. I'm still new to programming, so please use easy-to-understand explanations in your answers. Thank you!)
from npc_gen import NPC n1 = NPC() a = open("workfile", "wb") a.write(n1) a.close()I'm told:
Output:Unexpected type(s):
(NPC)
Possible types:
(bytearray)
(bytes)
How do I convert a (custom) class object into a bytes object? Pickle?Also, is this the best way to do what I want to do? I'm making my first app. Users will want to save some of the custom class objects that are created to their phones. I figured I'd need to have the program create a separate file to store those objects in. Is this best practice?
(Apologies if I have used any terms incorrectly or misunderstood something fundamental. I'm still new to programming, so please use easy-to-understand explanations in your answers. Thank you!)