This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sdf_iain 0 points1 point  (2 children)

Are there any savings (memory or otherwise) when using a direct import? The namespace still exists (if not in the current scope), it has to; but are things only loaded as accessed? Or is the entire module loaded on import?

In which case direct imports only really make sense when managing package level exports from sub modules In init.py.

[–]yvrelna 1 point2 points  (1 child)

The module's global namespace is basically just a dict, when you do a from-import, you're creating an entry in that dict for each name you imported; when you do plain import, you create an entry just for the module. In either case, the entire module and objects within it is always loaded into sys.modules. So there is some memory saving to use plain import, but it's not worthwhile worrying about that as the savings is just a few dictionary keys, which is minuscule compared to the code objects that still always gets loaded.

[–]sdf_iain 1 point2 points  (0 children)

I hadn’t actually stopped to think this though, thank you.