python中的全局变量警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20784758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:13:52  来源:igfitidea点击:

global variable warning in python

pythonpython-2.7globalpython-2.6

提问by whomaniac

I have a python 2.6 script (yes I know I should upgrade to at least 2.7) that looks like this:

我有一个 python 2.6 脚本(是的,我知道我应该升级到至少 2.7),看起来像这样:

ret_code = 0

def some_func()
  global ret_code
  ...

if __name__ == '__main__':
  global ret_code
  ...

Now I get a warning if I run the code: *SyntaxWarning: name 'ret_code' is assigned to before global declaration global ret_code*

现在,如果我运行代码,我会收到警告: *SyntaxWarning: name 'ret_code' isassigned to before global declaration global ret_code*

Why do I get this warning?

为什么我会收到此警告?

I can solve the problem by doing so:

我可以通过这样做来解决问题:

def some_func()
      global ret_code
      ...

if __name__ == '__main__':
  global ret_code
  ret_code = 0 #assign 0 here instead of above
  ...

Still that doesn't answer my question. What is wrong with the original code?

仍然没有回答我的问题。原始代码有什么问题?

采纳答案by lvc

The best direct way to fix this is to remove the globaldeclaration from underneath if __name__ == '__main__':. You don't need it there. Unlike some other languages, an ifstatement doesn't introduce a new scope in Python - any variables you assign in that block are global, just as they would be without the ifthere.

解决此问题的最佳直接方法是global从下面删除声明if __name__ == '__main__':。你在那里不需要它。与其他一些语言不同,if语句不会在 Python 中引入新的作用域——您在该块中分配的任何变量都是全局的,就像没有if那里一样。

This also explains why it is an error: to declare a variable as global, you are not allowed to have used that variable name previously in the same scope (presumably because it would be confusing for the globalstatement to make assignments beforeit go to the global variable, and Python doesn't support the same name being both global and local in the same scope). Since the ifdoesn't introduce a new scope, the first assignment to ret_codeis in the same scope as the global declaration under the if; but the global declaration comes later than the assignment, which isn't allowed.

这也解释了为什么会出错:将变量声明为global,不允许您之前在同一范围内使用过该变量名(大概是因为global语句转到全局变量之前进行赋值会造成混淆), 并且 Python 不支持在同一范围内同时具有全局和局部的相同名称)。由于if没有引入新的作用域,因此第一个赋值ret_codeif;下的全局声明在同一作用域内。但是全局声明晚于赋值,这是不允许的。

For an even better fix, consider if you can remove the mutable global state from your program entirely, since it usually causes more problems than its worth somewhere down the track. Here, it seems likely that you're using ret_codeas a program exit code - ie, you'll be doing sys.exit(ret_code)somewhere - but you're deciding inside a function which exit code to use. Instead, pass enough information back out to the top level code for itto decide which exit code to use.

为了获得更好的修复,请考虑是否可以从程序中完全删除可变全局状态,因为它通常会导致更多的问题,而不是它在轨道上的某个地方的价值。在这里,您似乎很可能将ret_code程序退出代码用作程序退出代码 - 即,您将在sys.exit(ret_code)某处执行操作 - 但您正在函数内部决定要使用哪个退出代码。相反,将足够的信息传回给顶级代码,以便决定使用哪个退出代码。