• <small id='6eouT'></small><noframes id='6eouT'>

  • <tfoot id='6eouT'></tfoot>

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

    <legend id='6eouT'><style id='6eouT'><dir id='6eouT'><q id='6eouT'></q></dir></style></legend>

        • <bdo id='6eouT'></bdo><ul id='6eouT'></ul>

      1. 如何从函数中更改全局变量?

        How to change a global variable from within a function?(如何从函数中更改全局变量?)

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

            • <legend id='BHBMg'><style id='BHBMg'><dir id='BHBMg'><q id='BHBMg'></q></dir></style></legend>
            • <small id='BHBMg'></small><noframes id='BHBMg'>

              <tfoot id='BHBMg'></tfoot>
                  <tbody id='BHBMg'></tbody>

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

                  本文介绍了如何从函数中更改全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试对定义的变量进行加减运算,但我不知道如何用新值覆盖旧值.

                  I'm trying to add or subtract from a defined variable, but I can't figure out how to overwrite the old value with the new one.

                  a = 15
                  
                  def test():
                      a = a +10
                      print ( a )
                  
                  test()
                  

                  错误信息:

                  Traceback (most recent call last):
                    File "test.py", line 7, in <module>
                      test()
                    File "test.py", line 4, in test
                      a = a +10
                  UnboundLocalError: local variable 'a' referenced before assignment
                  

                  推荐答案

                  您尝试运行代码时遇到的错误是:

                  The error that you get when you try to run your code is:

                  UnboundLocalError: local variable 'a' referenced before assignment
                  

                  ……从表面上看,这似乎很奇怪:毕竟,上面代码中的 first 语句(a = 15)是一个赋值.那么,发生了什么?

                  … which, on the face of it, seems strange: after all, the first statement in the code above (a = 15) is an assignment. So, what's going on?

                  实际上,发生了两件截然不同的事情,除非你已经知道它们,否则它们都不是显而易见的.

                  Actually, there are two distinct things happening, and neither of them are obvious unless you already know about them.

                  首先,你实际上有两个不同的变量:

                  First of all, you actually have two different variables:

                  • 第一行中的 a 是一个全局变量(之所以这么称呼,是因为它存在于全局范围内,在任何函数定义之外).

                  • The a in your first line is a global variable (so called because it exists in the global scope, outside of any function definitions).

                  其他行中的 a 是一个局部变量,这意味着它只存在于您的 test() 函数中.

                  The a in the other lines is a local variable, meaning that it only exists inside your test() function.

                  这两个变量完全不相关,即使它们同名.

                  These two variables are completely unrelated to each other, even though they have the same name.

                  如果在函数内部有一个语句分配给它,则该变量是函数的局部变量 - 例如,您的 a = a +10 行.

                  A variable is local to a function if there's a statement assigning to it inside that function - for instance, your a = a +10 line.

                  即便如此,这个错误看起来还是很奇怪——毕竟你在test()中做的第一件事就是赋值给a,那么怎么才能引用它呢?提前?

                  Even so, the error still looks strange - after all, the very first thing you do inside test() is assign to a, so how can it be referenced beforehand?

                  答案是,在赋值语句中,Python 会先评估 = 符号右侧的所有内容,然后再将其分配给左侧的名称 - 所以即使赋值是首先在你的代码中,a首先在右手边被引用:a +10.

                  The answer is that, in an assignment statement, Python evaluates everything on the right hand side of the = sign before assigning it to the name on the left hand side – so even though the assignment is written first in your code, a gets referenced first in that right hand side: a +10.

                  有两种方法可以解决这个问题.首先是告诉 Python 你真的希望 test() 中的 a 与全局范围内的 a 相同:

                  There are two ways you can get around this. The first is to tell Python that you really want the a inside test() to be the same a in the global scope:

                  def test():
                      global a
                      a = a + 10
                      print(a)
                  

                  这可行,但它是编写程序的一种非常糟糕的方式.更改函数内部的全局变量很难很快管理,因为您通常有很多函数,而且它们都无法确定另一个函数不会以他们不期望的方式弄乱全局变量.

                  This will work, but it's a pretty bad way to write programs. Altering global variables inside functions gets hard to manage really quickly, because you usually have lots of functions, and none of them can ever be sure that another one isn't messing with the global variable in some way they aren't expecting.

                  更好的方法是将变量作为参数传递给函数,如下所示:

                  A better way is to pass variables as arguments to functions, like this:

                  a = 15
                  
                  def test(x):
                      x = x + 10
                      print(x)
                  
                  test(a)
                  

                  请注意,名称不必相同 - 您对 test() 的新定义只是说它接受一个值,然后对其进行处理.你可以传入任何你喜欢的东西——它可以是a,或者数字7,或者别的什么.事实上,如果您尽量避免在不同范围内使用同名变量,您的代码总是会更容易理解.

                  Notice that the name doesn't have to be the same - your new definition of test() just says that it accepts a value, and then does something with it. You can pass in anything you like – it could be a, or the number 7, or something else. In fact, your code will always be easier to understand if you try to avoid having variables with the same name in different scopes.

                  如果您使用上面的代码,您会发现一些有趣的东西:

                  If you play with the code above, you'll notice something interesting:

                  >>> a = 15
                  >>> test(a)
                  25
                  >>> a
                  15
                  

                  a 没有改变!那是因为虽然你把它传递给 test() 并且它被分配给 x,然后 x 被改变了,留下原来的 a 单独.

                  a didn't change! That's because although you passed it into test() and it got assigned to x, it was then x that got changed, leaving the original a alone.

                  如果你想真正改变a,你需要从函数中返回你修改后的x,然后重新赋值给a在外面:

                  If you want to actually change a, you need to return your modified x from the function, and then reassign it back to a on the outside:

                  >>> a = 15
                  >>> 
                  >>> def test(x):
                  ...     x = x + 10
                  ...     print(x)
                  ...     return x
                  ... 
                  >>> a = test(a)
                  25
                  >>> a
                  25
                  

                  这篇关于如何从函数中更改全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

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

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

                        <tbody id='YCsal'></tbody>

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