So I have a class called cell:
class cell:
possibles = [ "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
value = None;
def __init__(self, value):
if value == "":
self.value = "0"
else:
self.value = value
if __name__=="__main__":
mlist = [cell("2"), cell("6"), cell("8")]
mlist[2].possibles.remove("3")
print mlist[0].possibles
The output is:
['1', '2', '4', '5', '6', '7', '8', '9']
Why would it remove a value from possibles in the first item of the array, when I explicitly removed it from the third item?
This is a common Python gotcha. The way this is written every
cellhas a reference to the same list instance. You can check this by printingid(mlist[i].possibles)for eachi.To create separate lists, move the initialization to the constructor: