Home > Software engineering >  In Python, why is my deepCopy still having its values changed?
In Python, why is my deepCopy still having its values changed?

Time:04-04

I have made a class in python and I am trying to create a deepCopy of it, however the 'deepCopy' still has its values changed when I modify the original.

class boardState():
    def __init__(self, board, orient,actions,expl):
        self.board
        self.orientations = orient
        self.actions = actions
        self.explored = expl    
    def __deepcopy__(self):
        return boardState(self.board, self.orientations, self.actions, self.explored)

    board = []
    orientations = {}           #this stores the orientations of the cars in the current problem.
    actions = []                #this is a list of the moves made to get to this current position.
    explored = [] 

^ above is the class that I am using and want to make a copy of.

 referencestate = copy.deepcopy(state)         
            
 print(id(referencestate))                                   
 print(id(state))  

^ after running this line, it's shown that they have the same id, I would like to make them Independant.

Any help would be greatly appreciated!

CodePudding user response:

I think this deepcopy just get a new class for boardState but the id(state.board) = id(referencestate.board), because the original object is used directly when creating the class . If you do not want to change the original value, do not pass the parameter directly.Use a copy of them. You can try to use ->state=boardState(board[:], dict.copy(orientations), actions[:], explored[:]) #this use shallow. Take a look at the code below ->

import copy
class boardState():
    def __init__(self, board, orient,actions,expl):
        self.board = board
        self.orientations = orient
        self.actions = actions
        self.explored = expl
        
    """
    def __deepcopy__(self, memo):
        return boardState(self.board[:], dict.copy(self.orientations), self.actions[:], self.explored[:])
    
    def __deepcopy__(self):
        return boardState(self.board, self.orientations, self.actions, self.explored)
    """
    def __deepcopy__(self, memo):
        dpcpy = self.__class__
        memo[id(self)] = dpcpy
        for attr in dir(self):
            if not attr.startswith('_'):
                value = getattr(self, attr)
                setattr(dpcpy, attr, copy.deepcopy(value, memo))
        return dpcpy


board = [[1]]
orientations = {"6":"h"}           #this stores the orientations of the cars in the current problem.
actions = [2]                #this is a list of the moves made to get to this current position.
explored = [3] 
state1=boardState(board, orientations, actions,     explored)
state2=boardState(board[:], dict.copy(orientations), actions[:],    explored[:])
referencestate = copy.deepcopy(state1)
print(id(referencestate))
print(id(state1))
print(id(state2))
print(id(state1.board))
print(id(state2.board))
print(id(state1.board) == id(state2.board))
print(id(referencestate.board))
print(id(state1.board) == id(referencestate.board))
print(id(state1.board[0]))
print(id(state2.board[0]))
print(id(state1.board[0]) == id(state2.board[0]))
print(id(referencestate.board[0]))
print(id(state1.board[0]) == id(referencestate.board[0]))

try running

  • Related