Syntax for Working with Classes--Need a bit of help

Please take a look at the code below, any advice will be greatly appreciated

I have two scripts one works as anticipated, the other does not.
The one that works is:

class Score():
    def __init__(self):
        self.score = 1
        self.num_enemies = 2
        self.num_lives = 3

    def set_score(self, num):
        self.score = num

    def set_enemies(self, num):
        self.num_enemies = num

    def set_lives(self, num):
        self.num_lives = num

    def get_score(self):
        return self.score

    def get_enemies(self):
        return self.num_enemies

    def get_lives(self):
        return self.num_lives


s = Score()

s.set_score(100)
s.set_enemies(200)
s.set_lives(300)

print(s.get_score())
print(s.get_enemies())
print(s.get_lives())

the ouput is:
100
200
300

The other one (where I do not assign score() to s does not update the output to the new values

class Score():
    def __init__(self):
        self.score = 1
        self.num_enemies = 2
        self.num_lives = 3

    def set_score(self, num):
        self.score = num

    def set_enemies(self, num):
        self.num_enemies = num

    def set_lives(self, num):
        self.num_lives = num

    def get_score(self):
        return self.score

    def get_enemies(self):
        return self.num_enemies

    def get_lives(self):
        return self.num_lives


Score().set_score(100)
Score().set_enemies(200)
Score().set_lives(300)

print(Score().get_score())
print(Score().get_enemies())
print(Score().get_lives())

output is:
1
2
3

Great demonstration of the problem! You’ve hit on what’s going on and made a good summary. Thank you for helping us to help you :slight_smile:

So the crucial difference is between these two blocks:

s = Score()
s.set_lives(300)
print(s.get_lives())
Score().set_lives(300)
print(Score().get_lives())

When you call Score(), what you’re doing is constructing a Score object (also called “instantiating the Score class”). Each time you do that, you get a completely new Score object with no memory of the previous ones. For the first example, you create one single Score object, then set three things on it, and finally retrieve those three from it; in the second, you construct six indepented Score objects - three of them get something set on them and then the object gets discarded, and the other three are built just to look up something, and are then discarded.

You can make use of this if you want to! Imagine you had a two-player game. You could…

player_one = Score()
player_two = Score()

and then update their scores and lives independently.

Hopefully that clears it up!

s = Score() creates an instance and binds it to s. You then call some of its methods that modify attributes.

Score().set_score(100) creates an instance and calls a method on it. The instance is then discarded.
Score().set_enemies(200) creates an instance and calls a method on it. The instance is then discarded.
2 separate instances, both have an attribute modified, both discarded.

1 Like

Chris & Mathew…
Thank you very much. I’ve read the python documentation on classes so your explanations make perfect sense. I just couldn’t get my head around just what was going on with regard to the instantiation. Appreciate your quick responses.

1 Like