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:
- When first encounter of
import config
happens, all assignment happens - When first variable in config is accessed, then all assignment happens
- 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?