Py学习  »  Python

在Python中,为什么deepCopy的值仍在更改?

Koby • 3 年前 • 1487 次点击  

我用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,我想让它们独立。

任何帮助都将不胜感激!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133959
 
1487 次点击  
文章 [ 1 ]  |  最新文章 3 年前