问题描述
我正在尝试对定义的变量进行加减运算,但我不知道如何用新值覆盖旧值.
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
这篇关于如何从函数中更改全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!