Home Ask Login Register

Developers Planet

Your answer is one click away!

Arkyris February 2016

Saving and Loading a Class Instance

I have a class that creates a new character object.

class Character():
    def __init__(self):
    self.name = None
    self.hp = None
    self.mp = None


class Race(Character):
    def __init__(self):
    super().__init__()
    self.race = None

That is located in a module called char. So I have my code for creating a new game.

    elif command == "new game":
        name_game = input('Name your game: ')

        cur_save=open('saves/' + name_game + '.p', 'wb')
        player = char.Character()
        player_name = input('Name your character: ')
        player.name = player_name
        player_race = input('What is your race? ')
        player.race = player_race
        pickle.dump(player, cur_save, -1)
        cur_save.close()

Now I want to start the game and load the players name and race that were entered. But the following just sets player to undefined.

    cur_save=open('saves/' + name_game + '.p', 'rb')
    player = pickle.load(cur_save)

I want it to set the player.name to the saved value and same with player.race. Is there something else I should be using like a database sort of thing (MySQL)

Answers


sheridp February 2016

So I made a quick test and have no problems:

import pickle
class Character():
    def __init__(self):
        self.name = None
        self.hp = None
        self.mp = None


class Race(Character):
    def __init__(self):
        super().__init__()
        self.race = None

name_game = input('Name your game: ')

cur_save=open('saves/' + name_game + '.p', 'wb')
player = Character()
player_name = input('Name your character: ')
player.name = player_name
player_race = input('What is your race? ')
player.race = player_race
pickle.dump(player, cur_save, -1)
cur_save.close()

cur_save=open('saves/' + name_game + '.p', 'rb')
player = pickle.load(cur_save)
print(player.name)

and what I get is:

%run temp.py
Name your game: test
Name your character: Brian
What is your race? human
Brian

Sometimes, I've seen things fail with pickle if the definition of your class has changed, or even if you redefine the class (maybe by running a bit of code again). For example, lets say I run the first bit of code as above, and then run

class Character():
    def __init__(self):
        self.name = None
        self.hp = None
        self.mp = None

again, essentially redefining Character. Then you can get unintuitive behavior:

In [17]: player.__class__
Out[17]: __main__.Character
In [18]: player.__class__ == Character
Out[18]: False

In [20]: player2 = Character()
In [21]: player2.__class__
Out[21]: __main__.Character
In [22]: player2.__class__ == Character
Out[22]: True

So you might want to make sure something like that isn't happening. Usually it doesn't matter, but if variable names change or something, it can throw you off.

Post Status

Asked in February 2016
Viewed 3,742 times
Voted 13
Answered 1 times

Search




Leave an answer


Quote of the day: live life