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

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

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

          <bdo id='cYKz0'></bdo><ul id='cYKz0'></ul>

        使用 += 但未附加列表时出现 UnboundLocalError

        时间:2023-09-28

          <bdo id='yQbmU'></bdo><ul id='yQbmU'></ul>

            1. <small id='yQbmU'></small><noframes id='yQbmU'>

                  <tbody id='yQbmU'></tbody>

                • <legend id='yQbmU'><style id='yQbmU'><dir id='yQbmU'><q id='yQbmU'></q></dir></style></legend><tfoot id='yQbmU'></tfoot>
                  <i id='yQbmU'><tr id='yQbmU'><dt id='yQbmU'><q id='yQbmU'><span id='yQbmU'><b id='yQbmU'><form id='yQbmU'><ins id='yQbmU'></ins><ul id='yQbmU'></ul><sub id='yQbmU'></sub></form><legend id='yQbmU'></legend><bdo id='yQbmU'><pre id='yQbmU'><center id='yQbmU'></center></pre></bdo></b><th id='yQbmU'></th></span></q></dt></tr></i><div id='yQbmU'><tfoot id='yQbmU'></tfoot><dl id='yQbmU'><fieldset id='yQbmU'></fieldset></dl></div>
                  本文介绍了使用 += 但未附加列表时出现 UnboundLocalError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不太明白下面两个类似代码的区别:

                  I do not quite understand the difference between the following two similar codes:

                  def y(x):
                      temp=[]
                      def z(j):
                          temp.append(j)
                      z(1)
                      return temp
                  

                  调用 y(2) 返回 [1]

                  def y(x):
                      temp=[]
                      def z(j):
                          temp+=[j]
                      z(1)
                      return temp
                  

                  调用 y(2) 返回 UnboundLocalError: local variable 'temp' referenced before assignment.为什么 + 运算符会产生错误?谢谢

                  calling y(2) returns UnboundLocalError: local variable 'temp' referenced before assignment. Why + operator generates the error? Thanks

                  推荐答案

                  回答标题,+和"append"的区别是:

                  Answer to the heading, the difference between + and "append" is:

                  [11, 22] + [33, 44,] 
                  

                  会给你:

                  [11, 22, 33, 44]
                  

                  和.

                  b = [11, 22, 33]
                  b.append([44, 55, 66]) 
                  

                  会给你

                  [11, 22, 33 [44, 55, 66]] 
                  

                  错误答案

                  这是因为当您对作用域中的变量进行赋值时,该变量将成为该作用域的本地变量,并隐藏外部作用域中任何类似命名的变量

                  This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope

                  这里的问题是 temp+=[j] 等于 temp = temp +[j].临时变量在分配之前在此处读取.这就是它给出这个问题的原因.这实际上包含在 python 常见问题解答中.

                  The problem here is temp+=[j] is equal to temp = temp +[j]. The temp variable is read here before its assigned. This is why it's giving this problem. This is actually covered in python FAQ's.

                  如需进一步阅读,请单击此处.:)

                  For further readings, click here. :)

                  这篇关于使用 += 但未附加列表时出现 UnboundLocalError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python中用于列表操作的plus和append有什么区别? 下一篇:用矩阵 B 附加矩阵 A

                  相关文章

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

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

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

                • <tfoot id='LHLmg'></tfoot>
                  • <bdo id='LHLmg'></bdo><ul id='LHLmg'></ul>