Source: http://andylin02.iteye.com/blog/841604
Conclusion: Python's global variables: int string, list, dic (map) can modify its value if there is global. Regardless of whether this global exists in the IF, or whether the if can be executed.
However, if there is no
python code
- If BGlobal:
- Global G_strval;
int string will be an error. The list dic (map) is OK.
Python code
- #!/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");
- 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:
= = = 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 ']
Testing of Python's global variables