简体   繁体   English

python中的全局变量Scope

[英]Global Variable Scope in python

Should I need to Global everything Global?我是否需要将一切都全球化? Considering scope?考虑 scope?

I keep randomly running into this problem, Im gonna assume its some how a syntax problem on myside.我一直随机遇到这个问题,我会假设它是我这边的语法问题。 But variables out side of a scope in python seems to be inconsistent... my situation is但是 python 中 scope 之外的变量似乎不一致......我的情况是

libFound=False

def Setup():
   _setup_import()
   print('booting:',libFound)
   #--Here I get False?
   if libFound:
      _boot()
   else:print('Did not Boot')

def _setup_import():
   sys.path.append(PATH)
try:
   import libwrapper
   global libwrapper
except:
   #Critical Exception
else:
   #found
   libFound=True
   print(libFound)#--Here I get True

I assume this is garbage collection but I would think it would match gloval before local variables, should I have to global everything global?我认为这是垃圾收集,但我认为它会在局部变量之前匹配 gloval,我是否必须将所有内容都全局化? Scope is scope, I seem to get this often in python. I would like to include it seems to happen most when initiateing with None or it being Bool Scope 是 scope,我似乎经常在 python 中得到这个。我想包括它似乎最常发生在用 None 启动时或者它是 Bool

Makeing variables global,checking syntax, I've tried researching this but I don't understand if its my syntax possibly or my lack or understanding of how python is actually handleing variable使变量全局化,检查语法,我已经尝试研究这个但我不明白它是否可能是我的语法或者我缺乏或不了解 python 如何实际处理变量

A global variable is, by definition, bound in the global scope. But assignments to names in a function always define a new local variable, unless you declare the name as global or non-local using global or nonlocal, respectively.根据定义,全局变量绑定在全局 scope 中。但是对 function 中的名称的赋值总是定义一个新的局部变量,除非您分别使用 global 或 nonlocal 将名称声明为全局或非局部。 – chepner 10 mins ago Boom... inside the scope of _setup_libraries() I made it global and it worked, like you said inside the scope of the method it was local – chepner 10 分钟前 Boom... 在 _setup_libraries() 的 scope 中我将它设为全局并且它有效,就像你在方法的 scope 中所说的那样它是本地的

This has nothing to do with the garbage collector.这与垃圾收集器无关。 I think you may misunderstand how scoping works in python.我认为您可能误解了 python 中的作用域是如何工作的。

See code & comments in it below.请参阅下面的代码和注释。 I hope it helps.我希望它有所帮助。

myGlobal = False

def ask_global():
    # To use Glboal Variable, we dont need to use global keyword
    print(myGlobal)

def toggle_global():
    # Changes the value of the global variable
    global myGlobal
    myGlobal = not myGlobal

def global_false():
    # Will not actually set myGlobal false, because scoping
    # When assigning a global without first declaring it as global, 
    # it will be a new local variable
    # myGlobal = not myGlobal
    # would result in 'myGlobal' referenced before assignment
    myGlobal = False
    

print(myGlobal) # False
ask_global() # False
if True:
    # We're in global scope here, changing the global variable
    myGlobal = True

print(myGlobal) # True
ask_global() # True

global_false()
print(myGlobal) # True
ask_global() # True

toggle_global()
print(myGlobal) # False
ask_global() # False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM