zoukankan      html  css  js  c++  java
  • [python自动化] 关于python无法修改全局变量的问题

    一直在工作也没空写blog..... =。=

    在编写python自动化程序的时候,会处理大量的参数。习惯性的处理方式是创建/configs/目录,在目录下存储各种参数。

    按参数功能来分,大约可以分为两类:

    1. 固定值的全局变量,如存储环境参数,特点是在整个程序中每次调用时,值固定

    2. 需要修改的全局变量,如存储pass/fail判定、时间戳等,特点是在程序过程中,会对其进行赋值

    # Record in file /configs/global_env.py
    PC_HOST = '10.0.0.2'
    PC_PORT = 8080
    # Global variable test
    from configs.global_env import *
    
    print 'PC Host: %s ' % PC_HOST
    print 'PC Port: %s ' % PC_PORT

    执行结果:

    PC Host: 10.0.0.2
    PC Port: 8080

    另外说一句,from configs.global_env import * 出错,需要在/configs/目录下建立 __init__.py文件,无内容即可

    当情况2时:

    # Record in file /configs/global_env.py
    BEGIN_TIME = None
    # Global variable test
    from configs.global_env import *
    import time
    
    class Modify_Global_Parameter(object):
        def init_begin_time(self):
            BEGIN_TIME = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(time.time()))
            print BEGIN_TIME
            return BEGIN_TIME
    
    if __name__ == '__main__':
        test = Modify_Global_Parameter()
        test.init_begin_time()
        print BEGIN_TIME

    执行结果

    2017-02-28_22:09:40
    None

    可以看出,在方法init_begin_time中print BEGIN_TIME,是修改后的结果

    但是调用方法结束后,再次打印,仍是原始值(None),这样便无法在其他类、方法中使用这个全局变量

    有两个解决方法:

    1. 方法1,使用global标识

    # Global parameter test
    from configs.global_env import *
    import time
    
    class Modify_Global_Parameter(object):
        def init_begin_time(self):
            global BEGIN_TIME
            BEGIN_TIME = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(time.time()))
            print BEGIN_TIME
    
    class Print_Test(object):
        def print_test(self, print_var):
            print BEGIN_TIME
            print print_var
    
    if __name__ == '__main__':
        test = Modify_Global_Parameter()
        test.init_begin_time()
        print BEGIN_TIME
        # test global variable as parameter.
        print_test = Print_Test()
        print_test.print_test(BEGIN_TIME)

    执行结果:

    2017-02-28_22:21:18
    2017-02-28_22:21:18
    2017-02-28_22:21:18
    2017-02-28_22:21:18

    可以发现,方法内修改后打印、方法外print、其他类中打印、作为其他方法的参数,均为修改后的结果

    方法2:使用字典存储

    # Record in file /configs/global_env.py
    TIME_STAMP = {
        'BEGIN_TIME': None,
        'END_TIME': None,
    }
    # Global parameter test
    from configs.global_env import *
    import time
    
    class Modify_Global_Parameter(object):
        def init_time_stamp(self):
            TIME_STAMP['BEGIN_TIME'] = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(time.time()))
            print TIME_STAMP['BEGIN_TIME']
    
    class Print_Test(object):
        def print_test(self, print_var):
            print TIME_STAMP['BEGIN_TIME']
            print print_var
    
    if __name__ == '__main__':
        test = Modify_Global_Parameter()
        test.init_time_stamp()
        print TIME_STAMP['BEGIN_TIME']
        # test global variable as parameter.
        print_test = Print_Test()
        print_test.print_test(TIME_STAMP['BEGIN_TIME'])

    执行结果:

    2017-02-28_22:30:15
    2017-02-28_22:30:15
    2017-02-28_22:30:15
    2017-02-28_22:30:15

    同样可以达到目标

    将参数存储在字典中,也易于分类和管理,我也更倾向于这种方法。

    另外,关于字典的一种打印:

    for key in TIME_STAMP:
        print '%s : %s' % (key, TIME_STAMP[key])

    执行结果:

    END_TIME : None
    BEGIN_TIME : 2017-02-28_22:35:48

    可以发现打印的key顺序与定义时的不同,这是因为字典的存储方法是Hash存储,并非顺序。

  • 相关阅读:
    O2O、B2B、C2C(通俗讲解)
    前端 $.parseJson()
    django反向解析传参
    从url(地址栏)获取参数:Jquery中getUrlParam()方法的使用
    Django:前后端分离后联调给前端传数据
    xpath 中 [<Element a at 3985984dj343>]
    sumafan:python爬虫多线程爬取数据小练习(附答案)
    window安装mysql(详细步骤)
    sqlserver从xlsx读取数据
    第一个kotlin程序
  • 原文地址:https://www.cnblogs.com/Liubit/p/6481419.html
Copyright © 2011-2022 走看看