python-为什么可以在主作用域中声明的函数中使用变量而不是全局变量 [英] python - why is it possible to use a variable in a function that is declared in the main scope and is not global
问题描述
我最近注意到python所没有的某些东西,可以检查/访问在主作用域中声明的函数内部的变量,例如:
I recently noticed something that I was not expected with python, it is possible to check/access a variable inside a function that is declared in the main scope, e.g.:
def my_func(mval):
print ("parameter:", mval)
print ("myvar:", myvar)
#myvar = "value" this is not allowed
print ("IN MAIN")
myvar = "123"
my_func(999)
输出:
IN MAIN
parameter: 999
myvar: 123
- 为什么
my_func
可以访问myvar
?然后为什么失败了试图改变价值? - 有没有办法防止这种行为,所以在这种情况下,它会说未定义变量
myvar
吗?
- Why
my_func
can access themyvar
? And then why it fails when trying to change the value? - Is there a way to prevent this behavior,
so in this case it would say variable
myvar
is not defined?
我知道我们可以使用 global
来完成这项工作,这是有道理的,因为我们明确表示要访问的函数范围之外还有一个变量.
I know we could use global
to make this work and it would make sense since we are explicitly saying there is a variable outside of the function scope that we want to access.
我发现这很棘手,因为它可能导致错误.
I find this tricky because it could lead to errors..
推荐答案
当您在 my_func
中显示 myvar
的内容时,使用您提供的确切代码作为参考,将检查全局变量表以查看它是否在执行中先前定义,这就是它起作用的原因.然后,当您尝试通过取消注释 myvar ="value"
在函数中为其分配值时,它将尝试将 myvar
定义为 my_func的局部变量
,它隐藏了对 myvar
的全局实例的引用.
When you display the content of myvar
in my_func
with the exact code you gave as reference, it will check the global variable table to see if it was previously defined in the execution, which is why it works. Then, when you try to assign a value to it in the function by uncommenting myvar = "value"
, it tries to define myvar
as a local variable of my_func
, which shadows the reference to the global instance of myvar
.
我不确定使用 global
关键字会引起什么问题,但是它应该可以正常工作.这是一个如何使用它的示例:
I'm not exactly sure how using the global
keyword would cause any issue, but it should be working as intended. Here is an example of how you can use it:
def my_func(mval):
global myvar
print ("parameter:", mval)
print ("myvar:", myvar)
myvar = "value"
print ("IN MAIN")
myvar = "123"
my_func(999)
print("myvar after my_func:", myvar)
输出:
IN MAIN
parameter: 999
myvar: 123
myvar after my_func: value
如果您真的不想使用 global
关键字,则可以通过将 myvar
作为 my_func
并返回修改后的值以在主范围中重新分配它:
If you really don't want to use the global
keyword, you can achieve the desired behavior by passing myvar
as a parameter of my_func
and return the modified value to reassign it in the main scope:
def my_func(mval, myvar):
print ("parameter:", mval)
print ("myvar:", myvar)
myvar = "value"
return myvar
print ("IN MAIN")
myvar = "123"
myvar = my_func(999, myvar)
print ("myvar after my_func:", myvar)
输出:
IN MAIN
parameter: 999
myvar: 123
myvar after my_func: value
如果已经构建了 my_func
返回一个值,请记住您可以在Python中返回多个值.因此,假设 my_func
将返回名为 returnvar
的变量,则上述代码可以写为:
If my_func
was already built to return a value, remember that you can return multiple values in Python. Therefore, assuming my_func
would return a variable called returnvar
, the above code could be written as:
def my_func(mval, myvar):
returnvar = mval + 1
print ("parameter:", mval)
print ("myvar:", myvar)
myvar = "value"
return myvar, returnvar
print ("IN MAIN")
myvar = "123"
myvar, returnvar = my_func(999, myvar)
print ("myvar after my_func:", myvar)
print ("returnvar:", returnvar)
输出:
IN MAIN
parameter: 999
myvar: 123
myvar after my_func: value
returnvar: 1000
这篇关于python-为什么可以在主作用域中声明的函数中使用变量而不是全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!