Explanation of python global Creation and Modification Example

  • 2021-11-30 00:36:38
  • OfStack

1. global Creating a global variable in a function is equivalent to creating a global variable in a file.

2. Modify the global variables in the function. Because global variables are allocated/modified directly in functions, they are considered to create new local variables.

Of course, you can modify elements in global variables by locating them through indexes.

Instances

global Variable 1, Variable 2, …
Variable 1 = Assignment data

# Error demonstration
The global variable expected to be modified is 1 = data # python assumes that a local variable was created: variable 1.

# Proper demonstration
global expects to modify the global variable 1 # first tell python that this variable is also seen as a global variable in this local area.
Global variable expected to be modified 1 = data

Content extension:

Global variables for Python: int, string, list, dic (map) global can be modified if it exists. Regardless of whether the global exists in the if or whether the if can be executed.

However, if there is no


if bGlobal: 
global g_strVal; 

int string will report an error. And list dic (map) is ok.


#!/usr/bin/dev python 
 
import sys 
import os 
 
g_nVal = 0; 
g_strVal = "aaaa"; 
 
g_map = { 
"aaa" : "111", 
"bbb" : "222", 
"ccc" : "333", 
"ddd" : "444" 
} 
 
g_ls = ['a', 'b', 'c'] 
 
def FixInt(bGlobal = False): 
  if bGlobal: 
    global g_nVal;   
     
  g_nVal = g_nVal + 1; 
   
def FixString(bGlobal = False): 
  if bGlobal: 
    global g_strVal; 
   
  #fix string value 
  g_strVal = g_strVal + 'b'; 
 
def FixMap(bGlobal = False): 
  if bGlobal: 
    global g_map; 
   
  #fix map value   
  g_map['aaa'] = 'aaa__' + g_strVal; 
  g_map['bbb'] = 'bbb__' + g_strVal; 
  g_map['ccc'] = 'ccc__' + g_strVal; 
  g_map['ddd'] = 'ddd__' + g_strVal; 
   
def FixList(bGlobal = False): 
  if bGlobal: 
    global g_ls; 
     
  g_ls.append('1');     
   
def PrintVal(strInfo): 
  if strInfo: 
    print("==== %s =====" %strInfo); 
     
  print("int value:%d" %g_nVal); 
  print("string value:%s" %g_strVal); 
  print("map value:%s" %g_map); 
  print("list value:%s" %g_ls); 
  print("\n\n");   
 
if "__main__" == __name__: 
   
  PrintVal("The orgin vlaue"); 
   
  FixInt(); 
  FixString(); 
  FixMap(); 
  FixList(); 
   
  PrintVal("print all bGlobal = False vlaue"); 
   
  FixInt(True); 
  FixString(True); 
  FixMap(True); 
  FixList(True); 
   
  PrintVal("print all bGlobal = True vlaue"); 

Results:


==== The orgin vlaue =====
int value:0
string value:aaaa
map value:{'aaa': '111', 'bbb': '222', 'ccc': '333', 'ddd': '444'}
list value:['a', 'b', 'c']

g_nVal src:0
g_nVal dst:1
==== print all bGlobal = False value =====
int value:1
string value:aaaab
map value:{'aaa': 'aaa__aaaab', 'bbb': 'bbb__aaaab', 'ccc': 'ccc__aaaab', 'ddd': 'ddd__aaaab'}
list value:['a', 'b', 'c', '1']

g_nVal src:1
g_nVal dst:2
==== print all bGlobal = True value =====
int value:2
string value:aaaabb
map value:{'aaa': 'aaa__aaaabb', 'bbb': 'bbb__aaaabb', 'ccc': 'ccc__aaaabb', 'ddd': 'ddd__aaaabb'}
list value:['a', 'b', 'c', '1', '1']


Related articles: