0

I have a python class called Player that looks like this ...

class Player:
    def __init__(self):
         self.display = 'A'
         self.num_water_buckets = 0
         self.row = 0
         self.col = 0

I am having trouble editing the self.row and self.col ... maybe they're immutable but I haven't found much of this on the internet. Player().row and Player().col work, however they print 0 every time. I am trying to edit them using Player().row = value where value is a variable containing a number.

Thanks in advance!

UPDATE: Okay so I understand that you have to define a new occurrence of this instance using x = Player() then x.row = ...

But I need to originally initialise the variable with certain values (which are different every time) so I thought I could use a function and the return value from that function would set the initial value from the variable but it just creates an infinite recursion. I did...

class Player:
def __init__(self):
    from game_parser import read_lines
    self.display = 'A'
    self.num_water_buckets = 0
    self.row = Player().detRow(sys.argv[1])
    self.col = Player().detCol(sys.argv[1])

def detRow(fileName):
    # use this function to return the row
def detCol(fileName):
    # use this function to return the col
5
  • Also I know there are other ways to do this like passing the variable in like def __init__(self, variable) then self.row = variable. Or something like that but it has to follow this structure Commented May 13, 2020 at 4:10
  • 1
    You have not shown how you are trying to "edit the self.row and self.col". Commented May 13, 2020 at 4:10
  • Sorry, I am trying to do it using Player().row = value where value is a variable containing a number Commented May 13, 2020 at 4:13
  • 1
    Player() is created each time, it's not the same object you retrieve the next time. You need to give it a name to have the same reference back to the same object. i.e. player = Player(); player.row = 1. When you retrieve player.row next time it will return 1. Commented May 13, 2020 at 4:16
  • Okay I understand this now, thank you, but I still need to change the initial value of it, if that makes sense. I updated the question that explains it a bit better. Commented May 13, 2020 at 5:36

2 Answers 2

1

If you do

Player().row = value
print(Player().row)

The Player() in the first row creates a new instance; then you modify it by setting its row attribute to value; then you throw away that instance, because you haven't bothered to remember it anywhere. Then in the next row, you create a new instance (where the initialiser sets the row attribute to zero).

Not all players are interchangeable. If you sit John in the second row, it does not mean Harry will be in the second row too; it just means John is in the second row.

To do it correctly, store your Player instance in a variable:

john = Player()
john.row = 2
print(john.row)
Sign up to request clarification or add additional context in comments.

3 Comments

oh okay that makes so much sense, you have to define a particular "instance" (occurrence) of the variables in the constructor. Thanks!
Not quite. 1) variables don't have instances, classes do; variables can contain or refer to instances (of classes). 2) your code does not have a constructor (__new__) — __init__ is an initialiser, as the name says; 3) none of what I said above (except setting the initial value of the attribute to zero) happens in initialiser (or constructor).
Ok okay, I'm confusing this with class attributes. Thank you :)
1

You need to add put the variable outside the __init__() method

EDIT: added row and col to the class

class Player:
    row = 0 # NO self here
    col = 0
    def __init__(self):
        self.display = 'A'
        self.num_water_buckets = 0

john = Player()
john.num_water_buckets = 12

print(f'before change: {john.col}')
john.col = 5
print(f'after change: {john.col}')
print(f'class property: {Player.col}') # note it did not change the class

Output:

before change: 0
after change: 5
class property: 0

2 Comments

Okay thank you. I am just confused because they want the row and col to be stored like that in the class (not as a separate instance) so I might have to set it equal to a function that returns the row and col or something. But thanks for the help :)
to define a "constant" variable for the class, move it out of the __init__ method, I'll update

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.