globvar = 0
def set_globvar_to_one():global globvar # Needed to modify global copy of globvarglobvar = 1
def print_globvar():print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()print_globvar() # Prints 1
var = "test"
def printGlobalText():global var #wWe are telling to explicitly use the global versionvar = "global from printGlobalText fun."print "var from printGlobalText: " + var
def printLocalText():#We are NOT telling to explicitly use the global version, so we are creating a local variablevar = "local version from printLocalText fun"print "var from printLocalText: " + var
printGlobalText()printLocalText()"""Output Result:var from printGlobalText: global from printGlobalText fun.var from printLocalText: local version from printLocalText[Finished in 0.1s]"""
def create_global_variable():global global_variable # must declare it to be a global first# modifications are thus reflected on the module's global scopeglobal_variable = 'Foo'
def use_local_with_same_name_as_global():# bad name for a local variable, though.global_variable = 'Baz'return global_variable + '!!!'
>>> use_local_with_same_name_as_global()'Baz!!!'
def declare_a_global_variable():global global_variable_1global_variable_1 = 1
# Note to use the function to global variablesdeclare_a_global_variable()
2.分配变量外部函数:
global_variable_2 = 2
现在我们可以在其他函数中使用这些声明的全局变量:
def declare_a_global_variable():global global_variable_1global_variable_1 = 1
# Note to use the function to global variablesdeclare_a_global_variable()global_variable_2 = 2
def print_variables():print(global_variable_1)print(global_variable_2)print_variables() # prints 1 & 2
global_variable_1 = 1global_variable_2 = 2
def update_variables():global global_variable_1global_variable_1 = 11global_variable_2 = 12 # will update just locally for this function
update_variables()print(global_variable_1) # prints 11print(global_variable_2) # prints 2
注2:
注释1对于列表和字典变量有一个例外,同时在函数内不使用全局行:
# declaring some global variablesvariable = 'peter'list_variable_1 = ['a','b']list_variable_2 = ['c','d']
def update_global_variables():"""without using global line"""variable = 'PETER' # won't update in global scopelist_variable_1 = ['A','B'] # won't update in global scopelist_variable_2[0] = 'C' # updated in global scope surprisingly this waylist_variable_2[1] = 'D' # updated in global scope surprisingly this way
update_global_variables()
print('variable is: %s'%variable) # prints peterprint('list_variable_1 is: %s'%list_variable_1) # prints ['a', 'b']print('list_variable_2 is: %s'%list_variable_2) # prints ['C', 'D']
def someFunc():x=20globals()['y']=50someFunc() # invoking function so that variable Y is created globallyprint(y) # output 50print(x) #NameError: name 'x' is not defined as x was defined locally within function
global_var = 10 # will be considered as a global variable
def func_1():global global_var # access variable using variable keywordglobal_var += 1
def func_2():global global_varglobal_var *= 2print(f"func_2: {global_var}")
func_1()func_2()print("Global scope:", global_var) # will print 22
Initialized = 0 #Here This Initialized is global variable
def Initialize():print("Initialized!")Initialized = 1 #This is local variable and assigning 1 to local variablewhile Initialized == 0:
这里我们比较全局变量初始化为0,所以循环条件为真
Initialize()
函数将被调用。循环将是无限的
#if we do Initialized=1 then loop will terminate
else:print("Lets do something else now!")