I was working with Python deep copy trying to create a total copy of original object, but the deep copy didn't seem to create a copy. It still shares the same reference with original object, which is not desired
Here's the code. I have a class Board
, the instance of which I want to deep copy.
import copy
class Board:
xpos = None
opos = None
count = None
status = []
def __init__(self, size):
self.xpos=[0,0]
self.opos=[size-1,size-1]
self.count = size*size-2
for i in range(size):
tmp = ['-']*size
self.status.append(tmp)
self.status[0][0] = 'X'
self.status[size-1][size-1]= 'O'
Somewhere in another function I want to call
board=Board()
localboard=copy.deepcopy(board)
# then do modification to local board....
# but it seems the old board is also affected. This is so weird since
# I am already using deep copy.
So how can I create a deep copy of the old board? I don't want to share any reference, since I will do modification on the local one and want to keep the old intact..