2

I'm having trouble understanding how to instantiate a class, and update that instances variables. If I __init__ a series of self.x variables, then instance that class, I want to update self.x = 40. However, self.x always stays constant.

I have a feeling I'm not wrapping my head around the class variable, init variable, and instanced class variables. I can always access them, I just can't seem to change them. I have coded an example of what I am trying to do.

class Engine(object):
    def __init__(self, board):
        self.board = board

    def play_game(self):
        print(self.board.sheet[0])
        number_one = int(input("Please enter a number."))
        self.board.a = number_one
        number_two = int(input("Please enter another number."))
        self.board.b = number_two
        number_three = int(input("Please enter a third number."))
        self.board.c = number_three
        number_four = int(input("Please enter a final number."))
        self.board.d = number_four

        print("Thank you! List updated.")

        print(self.board.sheet[0])


class ScoreBoard(object):
     def __init__(self):
        self.a = "_____"
        self.b = "_____"
        self.c = "_____"
        self.d = "_____"

        self.sheet = [f"""

            1. Number One:    {self.a}
            2. Number Two:    {self.b}
            3. Number Three:  {self.c}
            4. Number Four:   {self.d}
            """]

new_board = ScoreBoard()
new_game = Engine(new_board)
new_game.play_game()

When I print self.board.sheet[0] I would like to show the numbers instead of the lines for self.a through self.d.

iBug
  • 35,554
  • 7
  • 89
  • 134
Andrew
  • 21
  • 1
  • 3
  • What does *show the numbers instead of the lines* mean? – iBug Feb 09 '19 at 04:43
  • It should read: 1. Number One: 40 (from ```self.board.a = number_one```) not 1. Number One: _____ (this is the init value) – Andrew Feb 09 '19 at 04:48

1 Answers1

1

You need to recompute self.sheet after self.a through self.d are set. After self.sheet is assigned it just contains a simple string. That string isn't automatically updated when the fields are changed; you have to do it yourself.

Better yet, make sheet a method rather than a static variable.

class ScoreBoard(object):
    def __init__(self):
        self.a = "_____"
        self.b = "_____"
        self.c = "_____"
        self.d = "_____"

    def sheet(self):
        return f"""
            1. Number One:    {self.a}
            2. Number Two:    {self.b}
            3. Number Three:  {self.c}
            4. Number Four:   {self.d}
            """
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I like the return better. In trying this on a larger scale it does not work since it is not explicit which ```self.x``` to assign ```number_one```. I have to set a variable ```a_number = self.board.dict.get(number) # gets self.a - self.z``` and then set ```a_number = number_one```. At this point ```self.a``` is still ```"_____"``` and not the new number. – Andrew Feb 09 '19 at 05:23
  • This is because with `a_number = number_one`, you are reassigning the *reference* of `a_number` and not changing the *value* previously referenced by it. See: https://stackoverflow.com/questions/11222440/python-variable-reference-assignment – Nikolas Stevenson-Molnar Feb 09 '19 at 05:32
  • If you're still unclear why this is working, I would start a new question for that. – Nikolas Stevenson-Molnar Feb 09 '19 at 05:33