This is because you're declaring the equivalent of a static class variable and when you update it, it gets updated for every instance of that class. What you want is to declare instance variables like this:
class table:
def __init__(self):
self.h = 0
self.g = 0
Now when you instantiate the class like this: aux = table()
, it assigns the values to that instance of table - just the aux
variable.
Your move function will look like this:
move(x):
newTable = table()
newTable.g = x.g + 1
which will increment the value of newTable.g
, but not the value of x.g
. You'll also probably want to return the new table like this: return newTable
and you can then use it in other functions.
It's worth noting that in your code you never actually perform instantiation of the table class. This means that when you assign aux = table
, it just assigns a reference to the class to your variable aux
. To create an instance of a class, you call the class like you would a function - this is called a constructor - and it calls the class's __init__
method. So when I declare aux = table()
, the table __init__
method is executed and then the new instance of the class is returned.