简体   繁体   中英

How to access to the variables of the scope of a module called from the main module?

I need to access to the variables of the scope of a module imported in the main module from other module.

In my app, I've some modules in the same directory. One of these modules is a module that read a config_file.ini using configparser in order to load the configuration variables that use the other modules.

I don't want to import more than one time the module that manages the config_file.ini in order to avoid the issues of keep open twice the same file.

I'm not using functions nor clases in the module that manages the config_file.ini (only a function to update the file) but not to load the variables.

Graphical concept:

Module_A:

# !usr/bin/python3
# -*- coding: UTF-8 -*-
import module_B
import module_C
print(module_B.my_var)
print(module_C.my_var)

Module_B:

# !usr/bin/python3
# -*- coding: UTF-8 -*-
my_var = "This is the Module B"

Module_C:

# !usr/bin/python3
# -*- coding: UTF-8 -*-
my_var = "This is the Module C"
print(module_B.my_var)

I'm using Python3.x

Should I encapsulate them in a separate class? I'm not experienced in OOP :-(

Modules are only loaded once; subsequent imports reuse the already imported module .

In other words, just load your config.ini in your one module, then import the variables from that module into others.

如果要访问Module_C中的Module_B.my_var,则需要在Module_C中import Module_B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM