Loading

Python 当中from ... import ... ,在原模块当中改变值并不改变引入后的符号值的问题

https://stackoverflow.com/questions/3536620/how-to-change-a-module-variable-from-another-module

from config import *
print(HAHA)
config.init_configs()
print(HAHA)

最好使用local variable

import config
config.init_configs()
HAHA = config.modify_HAHA()
print(HAHA)

# ...在local变量域使用HAHA
# all of the configurations are stored explicitly in this globally visible dict
# global dict can preserve the variable as the code is alive, which can save time for re-initing and reloading
# However, we should avoid directly accessing the value inside a module, i.e. `from ... import some_preserved_alive_global_value`
config_dict = {}

def get_configs(specific_yaml_path="exp.yaml"):
    # renaming for explicity
    global config_dict
    # modify the global dict
    
    # return the var, use it in another scope/context
    return config_dict
posted @ 2022-02-23 17:12  ZXYFrank  阅读(154)  评论(0编辑  收藏  举报