Modify the function of global variables

# Share data between functions, global variable modifications 
# 1, the use of global variables
# 2, the return value of
a global variable is a variable type (list, dict, set), without modifying the global variable declaration globle function in

g_num = 1 # defined global variable
DEF modify_value1 ():
global g_num # modify global variables the same variables are declared first, and then modify
g_num = 10 # use without first global declaration, a local variable is defined

print ( 'before modification:', g_num)
modify_value1 ( ) # call function to modify global variables
print ( 'modified:', g_num)

DEF modify_value2 ():
global g_num
g_num = 20 is
return g_num

DEF Show ():
value = modify_value2 ()
Print (value)

Show ()

Guess you like

Origin www.cnblogs.com/wjun0/p/11515386.html