Testing of Python's global variables

Source: Internet
Author: User

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
    1. If BGlobal:
    2. Global G_strval;

int string will be an error. The list dic (map) is OK.

Python code
  1. #!/usr/bin/dev python
  2. Import Sys
  3. Import OS
  4. G_nval = 0;
  5. G_strval = "AAAA";
  6. G_map = {
  7. "AAA": "111",
  8. "BBB": "222",
  9. "CCC": "333",
  10. "DDD": "444"
  11. }
  12. G_ls = [' A ', ' B ', ' C ']
  13. def fixint (bglobal = False):
  14. if BGlobal:
  15. Global G_nval;
  16. G_nval = G_nval + 1;
  17. def fixstring (bglobal = False):
  18. if BGlobal:
  19. Global G_strval;
  20. #fix String Value
  21. G_strval = G_strval + ' B ';
  22. def fixmap (bglobal = False):
  23. if BGlobal:
  24. Global G_map;
  25. #fix Map Value
  26. g_map[' aaa '] = ' aaa__ ' + g_strval;
  27. g_map[' bbb '] = ' bbb__ ' + g_strval;
  28. g_map[' CCC '] = ' ccc__ ' + g_strval;
  29. g_map[' ddd '] = ' ddd__ ' + g_strval;
  30. def fixlist (bglobal = False):
  31. if BGlobal:
  32. Global G_ls;
  33. G_ls.append (' 1 ');
  34. def printval (Strinfo):
  35. if Strinfo:
  36. print ("= = =%s ====="%strinfo);
  37. print ("int value:%d"%g_nval);
  38. Print ("string value:%s"%g_strval);
  39. print ("map value:%s"%g_map);
  40. Print ("list value:%s"%g_ls);
  41. print ("\ n");
  42. If "__main__" = = __name__:
  43. Printval ("The Orgin Vlaue");
  44. Fixint ();
  45. Fixstring ();
  46. Fixmap ();
  47. Fixlist ();
  48. Printval ("Print all BGlobal = False Vlaue");
  49. Fixint (True);
  50. Fixstring (True);
  51. Fixmap (True);
  52. Fixlist (True);
  53. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.