zoukankan      html  css  js  c++  java
  • python之全局变量的测试

    来源:http://andylin02.iteye.com/blog/841604

    结论:python的全局变量:int string, list, dic(map) 如果存在global就能够修改它的值。而不管这个global是否是存在于if中,也不管这个if是否能够执行到。

     

    但是,如果没有

    Python代码  收藏代码
    1. if bGlobal:  
    2.        global g_strVal;  

    int string 将会报错。而list dic(map)是ok的。

     

    Python代码  收藏代码
    1. #!/usr/bin/dev python  
    2.   
    3. import sys  
    4. import os  
    5.   
    6. g_nVal = 0;  
    7. g_strVal = "aaaa";  
    8.   
    9. g_map = {  
    10. "aaa" : "111",  
    11. "bbb" : "222",  
    12. "ccc" : "333",  
    13. "ddd" : "444"  
    14. }  
    15.   
    16. g_ls = ['a', 'b', 'c']  
    17.   
    18. def FixInt(bGlobal = False):  
    19.     if bGlobal:  
    20.         global g_nVal;      
    21.           
    22.     g_nVal = g_nVal + 1;  
    23.       
    24. def FixString(bGlobal = False):  
    25.     if bGlobal:  
    26.         global g_strVal;  
    27.       
    28.     #fix string value  
    29.     g_strVal = g_strVal + 'b';  
    30.   
    31. def FixMap(bGlobal = False):  
    32.     if bGlobal:  
    33.         global g_map;  
    34.       
    35.     #fix map value      
    36.     g_map['aaa'] = 'aaa__' + g_strVal;  
    37.     g_map['bbb'] = 'bbb__' + g_strVal;  
    38.     g_map['ccc'] = 'ccc__' + g_strVal;  
    39.     g_map['ddd'] = 'ddd__' + g_strVal;  
    40.       
    41. def FixList(bGlobal = False):  
    42.     if bGlobal:  
    43.         global g_ls;  
    44.           
    45.     g_ls.append('1');          
    46.       
    47. def PrintVal(strInfo):  
    48.     if strInfo:  
    49.         print("==== %s =====" %strInfo);  
    50.           
    51.     print("int value:%d" %g_nVal);  
    52.     print("string value:%s" %g_strVal);  
    53.     print("map value:%s" %g_map);  
    54.     print("list value:%s" %g_ls);  
    55.     print(" ");      
    56.   
    57. if "__main__" == __name__:  
    58.       
    59.     PrintVal("The orgin vlaue");  
    60.       
    61.     FixInt();  
    62.     FixString();  
    63.     FixMap();  
    64.     FixList();  
    65.       
    66.     PrintVal("print all bGlobal = False vlaue");  
    67.       
    68.     FixInt(True);  
    69.     FixString(True);  
    70.     FixMap(True);  
    71.     FixList(True);  
    72.       
    73.     PrintVal("print all bGlobal = True vlaue");  
    74.       
    75.       
    76.   
    77.       

    结果:

    ==== 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']


  • 相关阅读:
    .net core读取appsettings.config中文乱码问题
    vs2017错误:当前页面的脚本发生错误
    VS Code中无法识别npm命令
    Visual Studio报错/plugin.vs.js,行:1074,错误:缺少标识符、字符串或数字
    记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法
    Java集合框架
    java hash表
    Java Dictionary 类存储键值
    java数据结构 栈stack
    java封装
  • 原文地址:https://www.cnblogs.com/stevenzeng/p/5234878.html
Copyright © 2011-2022 走看看