全局和局部变量在这种情况下的表现如何? [英] How do global and local variables behave in this case?
问题描述
保存在我的文件 function_local.py
:
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
输出:
Output:
$ python function_local.py
x is 50
Changed local x to 2
x is still 50
问题:当我们在Function中打印第一行时,为什么它打印出50,而不是2?即使这样说,在函数中, x = 2
?
Question: When we print the first line inside the Function, why does it print out 50, not 2? Even if it is said, in the function, that x = 2
?
推荐答案
如果您在函数中分配了一个变量名称(未声明 global
或 nonlocal
)或者在函数的参数列表中使用变量名称,变量名称将成为函数的一部分。
In case you assign to a variable name (that wasn't declared global
or nonlocal
) in a function or use the variable name in the argument list of the function the variable name will become part of the function.
在这种情况下,您可以在函数内使用任何变量名称,因为它始终会引用传入的局部变量:
In that case you could've used any variable name inside the function because it will always refer to the local variable that was passed in:
x = 50
def func(another_x):
print('local x =', another_x)
another_x = 2
print('local x =', another_x)
return another_x
print('global x =', x)
x = func(x) # note that I assigned the returned value to "x" to make the change visible outside
print('global x =', x)
更多解释
我会试着展示我以前的意思,当我说 x
with将成为函数的一部分。
More explanation
I'm going to try to show what I meant earlier when I said that x
with "will become part of the function".
__ code __。co_varnames $ c $函数的c>包含函数的局部变量名称列表。所以让我们看看在几种情况下会发生什么:
The __code__.co_varnames
of a function holds the list of local variable names of the function. So let's see what happens in a few cases:
如果它是签名的一部分:
If it's part of the signature:
def func(x): # the name "x" is part of the argument list
pass
print(func.__code__.co_varnames)
# ('x',)
如果分配给变量(函数中的任何位置) p>
If you assign to the variable (anywhere in the function):
def func():
x = 2 # assignment to name "x" inside the function
print(func.__code__.co_varnames)
# ('x',)
您只需访问变量名称:
If you only access the variable name:
def func():
print(x)
print(func.__code__.co_varnames)
# ()
在这种情况下,它实际上将查找外部作用域中的变量名称,因为变量名 x
不是函数varnames的一部分!
In this case it will actually look up the variable name in the outer scopes because the variable name x
isn't part of the functions varnames!
我可以理解这会如何混淆你,因为只需添加一个 x =
I can understand how this could confuse you because just by adding a x=<whatever>
anywhere in the function will make it part of the function:
def func():
print(x) # access
x = 2 # assignment
print(func.__code__.co_varnames)
# ('x',)
在这种情况下,不会查找变量 x
,因为它是现在函数的一部分,您将得到一个告诉异常(因为即使 x
是
In that case it won't look up the variable x
from the outer scope because it's part of the function now and you'll get a tell-tale Exception (because even though x
is part of the function it isn't assigned to yet):
>>> func()
UnboundLocalError: local variable 'x' referenced before assignment
显然,它会如果您在分配给它后访问它:
Obviously, it would work if you access it after assigning to it:
def func():
x = 2 # assignment
print(x) # access
或者如果您在调用函数时将其传入:
Or if you pass it in when you call the function:
def func(x): # x will be passed in
print(x) # access
关于局部变量名称的另一个重点是您不能从外部范围设置变量,除非您明确告诉Python使用全局
或非本地$
Another important point about local variable names is that you can't set variables from outer scopes, except when you tell Python to explicitly not make x
part of the local variable names, for example with global
or nonlocal
:
def func():
global x # or "nonlocal x"
print(x)
x = 2
print(func.__code__.co_varnames)
# ()
这实际上会覆盖全球(o当你调用 func()
!
This will actually overwrite what the global (or nonlocal) variable name x
refers to when you call func()
!
这篇关于全局和局部变量在这种情况下的表现如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!