I have code that contains a class with an list attribute. I am trying to make a copy of the list that I can edit, without modifying the orignal.
class Checkers():
def __init__(self):
# -1 red, 0 empty, 1 black
self.BOARD = [
[ 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0],
[ 0, -1, 0, -1, 0, -1, 0, -1],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 0, 1, 0, 1, 0, 1, 0],
[ 0, 1, 0, 1, 0, 1, 0, 1],
[ 1, 0, 1, 0, 1, 0, 1, 0],
]
def select(self):
temp = list(self.BOARD)
temp[0][0] = 'x'
temp = list(self.BOARD)
print(self.BOARD)
if __name__ == '__main__':
test = Checkers()
test.select()
When the print statement is called, it prints a list with the first element of 'x' instead of the original BOARD list. What can I do to create a copy that does not point to the original list? I have tried importing the copy library and using copy.copy() and slicing: self.BOARD[:]