社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Koby • 4 年前 • 2070 次点击  

我用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
文章 [ 1 ]  |  最新文章 4 年前