Why does Python load imported modules separately for different files, unlike C or PHP? Isn’t that inefficient in terms of memory usage?
27th October 2010
My answer to Why does Python load imported modules separately for different files, unlike C or PHP? Isn’t that inefficient in terms of memory usage? on Quora
It doesn’t—you’re misunderstanding how Python’s module system works. If two different places have “import os” in them, the os module is only imported and executed once—it’s cached in the sys.modules dictionary so you can see it happen if you want to. The key thing to understand is that “import os” attaches the os module to the “os” symbol within the current file’s scope, loading it only if it hasn’t been loaded already.
More recent articles
- ChatGPT Canvas can make API requests now, but it's complicated - 10th December 2024
- I can now run a GPT-4 class model on my laptop - 9th December 2024
- Prompts.js - 7th December 2024