Module initialization | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Module initialization

Someone pls explain this to me with examples. "The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded again, so local variables inside the module act as a "singleton," meaning they are initialized only once."

9th Sep 2022, 4:40 AM
Sara Silvers
Sara Silvers - avatar
1 Answer
+ 1
How I understand it: Let's say you have module a.py: t = 1 print(t) module b.py: import a u = 2 print(u) And then main module: import a import b print('main') ____________ >>> 1 >>> 2 >>> main Notice that '1' is not printed twice. I don't have it verified anywhere so that is just how I understand what you wrote here.
9th Sep 2022, 7:16 AM
Zuziss
Zuziss - avatar