I was doing some work in Python when I ran into an unexpected problem. I have a class, and two variables x and y. x and y are meant to be separate copies of the class each with their own data. So I set x equal to the class and give it some values, then do the same with y. Once I try to use that data though, I realize that I overwrote my values for x. It seems that I have not created two separate copies of the class to work with but instead two references to the same class. So here is the general example in code:
x = someClass()
x.set(2, 0)
y = someClass()
y.set(3, 0)
print(x)
The end result is that 3 is printed instead of 2. I want to create a separate "version" of the class to hold different data for each variable. Not sure as to how though. Using Python 3.3. Here is the code for the class:
class someClass:
def __init__(self, list = [0,0,0,0,0,0,0,0,0,0]):
self.list = list
def __repr__(self):
return str(self.list)
def set(self, loc, val):
if ((loc >= 0) & (loc <= 9)):
self.list[loc] = val