在 Python 中,为什么我的 deepCopy 的值仍在更改?
In Python, why is my deepCopy still having its values changed?
我在 python 中制作了一个 class,我正在尝试创建它的深层副本,但是当我修改原始文件时,'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 = []
^ 上面是我正在使用并想复制的class。
referencestate = copy.deepcopy(state)
print(id(referencestate))
print(id(state))
^ 在运行这一行之后,显示他们有相同的id,我想让他们独立。
如有任何帮助,我们将不胜感激!
我觉得这个deepcopy只是获取了一个新的class for boardState but the id(state.board) = id(referencestate.board), 因为创建的时候直接使用了原来的对象class 。
如果你不想改变原来的值,就不要传递参数directly.Use他们的副本。你可以尝试使用 ->state=boardState(board[:], dict.copy(orientations), actions[:], explored[:]) #this use 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]))
尝试运行
我在 python 中制作了一个 class,我正在尝试创建它的深层副本,但是当我修改原始文件时,'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 = []
^ 上面是我正在使用并想复制的class。
referencestate = copy.deepcopy(state)
print(id(referencestate))
print(id(state))
^ 在运行这一行之后,显示他们有相同的id,我想让他们独立。
如有任何帮助,我们将不胜感激!
我觉得这个deepcopy只是获取了一个新的class for boardState but the id(state.board) = id(referencestate.board), 因为创建的时候直接使用了原来的对象class 。 如果你不想改变原来的值,就不要传递参数directly.Use他们的副本。你可以尝试使用 ->state=boardState(board[:], dict.copy(orientations), actions[:], explored[:]) #this use 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]))
尝试运行