1. <legend id='Xhhkr'><style id='Xhhkr'><dir id='Xhhkr'><q id='Xhhkr'></q></dir></style></legend>

    <small id='Xhhkr'></small><noframes id='Xhhkr'>

    <tfoot id='Xhhkr'></tfoot>
      • <bdo id='Xhhkr'></bdo><ul id='Xhhkr'></ul>
      <i id='Xhhkr'><tr id='Xhhkr'><dt id='Xhhkr'><q id='Xhhkr'><span id='Xhhkr'><b id='Xhhkr'><form id='Xhhkr'><ins id='Xhhkr'></ins><ul id='Xhhkr'></ul><sub id='Xhhkr'></sub></form><legend id='Xhhkr'></legend><bdo id='Xhhkr'><pre id='Xhhkr'><center id='Xhhkr'></center></pre></bdo></b><th id='Xhhkr'></th></span></q></dt></tr></i><div id='Xhhkr'><tfoot id='Xhhkr'></tfoot><dl id='Xhhkr'><fieldset id='Xhhkr'></fieldset></dl></div>

      Python copy.deepcopy() 函数无法正常工作

      Python copy.deepcopy() function not working properly(Python copy.deepcopy() 函数无法正常工作)
      <i id='YoXtu'><tr id='YoXtu'><dt id='YoXtu'><q id='YoXtu'><span id='YoXtu'><b id='YoXtu'><form id='YoXtu'><ins id='YoXtu'></ins><ul id='YoXtu'></ul><sub id='YoXtu'></sub></form><legend id='YoXtu'></legend><bdo id='YoXtu'><pre id='YoXtu'><center id='YoXtu'></center></pre></bdo></b><th id='YoXtu'></th></span></q></dt></tr></i><div id='YoXtu'><tfoot id='YoXtu'></tfoot><dl id='YoXtu'><fieldset id='YoXtu'></fieldset></dl></div>
        • <bdo id='YoXtu'></bdo><ul id='YoXtu'></ul>

            <tbody id='YoXtu'></tbody>

          <legend id='YoXtu'><style id='YoXtu'><dir id='YoXtu'><q id='YoXtu'></q></dir></style></legend>

          • <tfoot id='YoXtu'></tfoot>

              <small id='YoXtu'></small><noframes id='YoXtu'>

              • 本文介绍了Python copy.deepcopy() 函数无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直在使用 deepcopy 函数和 copy 函数,但我遇到了同样的问题.就像副本是引用(或指针)而不是正确的副本.我正在使用 Python 处理数据记录(类),也许是这样.我给你看一个例子:

                I have been playing with the deepcopy function and the copy function and I get the same issue with both of them. It is like the copy was a reference (or a pointer) instead of a proper copy. I am working with data records (classes) in Python, maybe it could be that.. I show you an example:

                >>> import copy
                >>> class player1:
                ...    age = 23
                ...    score = 1
                >>> class player2:
                ...    age = 14
                ...    score = 2
                >>> player3 = copy.deepcopy(player1)
                

                我打印参数.

                >>> print player1.age, player1.score
                23 1
                >>> print player2.age, player2.score
                14 2
                >>> print player3.age, player3.score
                23 1
                

                现在我增加 player1 数据记录中的 score 参数.

                Now I increment the score parameter in player1 data record.

                >>> player1.score += 3
                

                然后我再次打印结果.

                >>> print player1.age, player1.score
                23 4
                >>> print player2.age, player2.score
                14 2
                >>> print player3.age, player3.score
                23 4 
                

                为什么玩家 3 发生了变化?我只是在 player1 中增加了一个参数,而不是 player3.它是可变的而不是不可变的.

                WHY HAS PLAYER 3 CHANGED? I just incremented a parameter in player1, not player3. It is mutable instead of immutable.

                提前致谢.

                推荐答案

                问题是你实际上是在复制类定义而不是类的实例.

                The problem is that you are actually copying the class definition and not an instance of the class.

                代码的另一个问题是属性 agescore 是类的一部分,将在该类的所有实例之间共享.这可能不是您想要的.

                Another problem of the code is that the attributes age and score are part of the class and will be shared between all instances of that class. This is probably not what you intended.

                你可能想做的是:

                import copy
                class Player:
                    def __init__(self, age, score):
                        self.age = age
                        self.score = score
                
                player1 = Player(23, 1)
                player2 = Player(14, 2)
                player3 = copy.deepcopy(player1)
                
                player1.age += 1
                
                print "player1.age", player1.age
                print "player3.age", player3.age
                

                这会满足您的期望:

                player1.age 24
                player3.age 23
                

                这篇关于Python copy.deepcopy() 函数无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    <tbody id='bOQXw'></tbody>
                  <tfoot id='bOQXw'></tfoot>
                    <bdo id='bOQXw'></bdo><ul id='bOQXw'></ul>
                  • <legend id='bOQXw'><style id='bOQXw'><dir id='bOQXw'><q id='bOQXw'></q></dir></style></legend>
                      1. <i id='bOQXw'><tr id='bOQXw'><dt id='bOQXw'><q id='bOQXw'><span id='bOQXw'><b id='bOQXw'><form id='bOQXw'><ins id='bOQXw'></ins><ul id='bOQXw'></ul><sub id='bOQXw'></sub></form><legend id='bOQXw'></legend><bdo id='bOQXw'><pre id='bOQXw'><center id='bOQXw'></center></pre></bdo></b><th id='bOQXw'></th></span></q></dt></tr></i><div id='bOQXw'><tfoot id='bOQXw'></tfoot><dl id='bOQXw'><fieldset id='bOQXw'></fieldset></dl></div>
                      2. <small id='bOQXw'></small><noframes id='bOQXw'>