Objective
Python stores the modules that have already been imported in the sys.modules dictionary: if you import one again, it is not run again, it is reused. Reproduce that mechanism with your own dictionary loaded.
Instructions
- Use the
SOURCESdictionary from the statement in your code:"alfa": ... , "beta": ...is the code of each module, and both showLoading namewhen run and defineVALOR. - Create an empty dictionary
loadedand writeimport_module_once(name): if the module is already inloaded, return it; otherwise create a module withtypes.ModuleType(name), store it inloadedand run its code withexec(code, module.__dict__). - Read
nand thennmodule names. If it is not inSOURCES, showUnknown module: name. - Otherwise import the module and show
name.VALOR = value.
4
alfa
beta
alfa
alfa
Loading alfa
alfa.VALOR = 10
Loading beta
beta.VALOR = 20
alfa.VALOR = 10
alfa.VALOR = 10
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise