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.

2
  • What does show the numbers instead of the lines mean? Commented Feb 9, 2019 at 4:43
  • It should read: 1. Number One: 40 (from self.board.a = number_one) not 1. Number One: _____ (this is the init value) Commented Feb 9, 2019 at 4:48

1 Answer 1

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}
            """
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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: stackoverflow.com/questions/11222440/…
If you're still unclear why this is working, I would start a new question for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.