在Python中,为什么我的DeepCopy仍然具有其值的变化?

发布于 2025-01-18 20:19:24 字数 886 浏览 4 评论 0原文

我在 python 中创建了一个类,并且尝试创建它的 deepCopy,但是当我修改原始类时,“deepCopy”的值仍然发生了变化。

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 = [] 

^ 上面是我正在使用并想要复制的类。

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

^ 运行此行后,显示它们具有相同的 id,我想让它们独立。

任何帮助将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2025-01-25 20:19:25

我认为这个deepcopy只是得到了一个新的boardState类,但是id(state.board) = id(referencestate.board),因为在创建class时直接使用原始对象。
如果不想更改原始值,请不要直接传递参数。使用它们的副本。你可以尝试使用 ->state=boardState(board[:], dict.copy(orientations), actions[:],explored[:]) #this 使用shallow。
看看下面的代码->

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]))

尝试跑步

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文