0

I have a general doubt around assignment of values to variables vs their loading. Does this happen for all variables (whose scope is global for a script) as soon as the script is loaded or only when they are referenced. I'll explain what I'm thinking using an example:

Lets say we have a config.py with just config vars as contents:

config.py:

VAR1 = "VALUE1"
VAR2 = "VALUE2"
VAR3 = "VALUE3"
.
.
VARn = "VALUEn"

This is a stand-alone script technically. However, in my use case, I have a project structure as:

project-root
 |--file1.py
 |--file2.py
 |--main.py
 |--config.py
 |--subdir
     |-f1
     |-f2

Now, all config-constants are defined in config.py and this is imported and used in all other python scripts in the project.

My question is, as soon as I run my project through main.py, when will all vars defined in config.py be assigned? Possible answers that I can think of:

  1. When first encounter of import config happens, all assignment happens
  2. When first variable in config is accessed, then all assignment happens
  3. Whenever any config-constant is first accessed from config.py, then that one only is assigned. Others will not be. (lazy-assignment, per first access)

Which of this is true?

vish4071
  • 5,135
  • 4
  • 35
  • 65
  • 1
    The variables in the `config.py` module will be created the first time it is imported. If another script as imports it, the results of the first execution will be used and it will not be run again. – martineau May 24 '22 at 08:35
  • @martineau Can you post this as answer so I'll accept it? Also, if you can add details if there are functions or 3rd party get_value calls and elaborate that with supporting doc. Will those also be loaded at import? – vish4071 May 24 '22 at 09:23

1 Answers1

0

When a module is first imported, Python searches for the module and if found, it creates a module object and initializes it by running the code in it. All top-level variables, functions, and class definitions executed will be saved as the contents of the module object. This includes calls to, or instances created, of the latter two that occur subsequent to their definition during execution of the top level of the module. (This would also include any "get value" calls made to third-party modules after they themselves have been imported.)

This only happens once because all module objects created get stored in a dictionary named sys.modules that maps module names to modules which have already been loaded. Additional attempts to import a module with the same name will be satisfied by utilizing the module object in the dictionary. It's effectively a temporary storage space to allow fast access to them — i.e. it's a cache. As a result, modules are typically only ever executed once even if they might be imported multiple times during the execution of the main script (or other scripts that are run as a side-effect of its execution).

The import system in general is described in this section of the documentation. Of course it also contains information specifically about the import statement.

martineau
  • 119,623
  • 25
  • 170
  • 301