My question arise from two principles that I follow. Sometimes they get in conflict:
I often read that when importing from a module (like PyQt5 for example) I should only import what I actually need. Which makes a lot of sense, hence that is what I do.
Then I also read a lot on the benefits of namespaces and this also makes a lot of sense.
Here comes the problem I encounter:
Let's say I need to do import os
because I need the module to do this: os.path.dirname(__file__)
and this is the only thing I use "os" module for, I would be tempted to import this function only (from os.path import dirname
).
If seems like a better import to me since I am only importing the function I actually need but then when I call dirname(__file__)
later in my code I have completly lost the notion of namespace.
Is there a way to import only one part of a module while keeping the obligation to call it with the module name?
I know I can do something like from os.path import dirname as os_path_dirname
.
But this doesn't feel right...
Please note that this question has nothing to do with PyQt5 nor the os module, those are just examples that came to my mind, but this is more about the general concept of losing the notion of namespaces if I import only specific things.
My guess right now is that some modules (like "os") are very well written hence calling import os
doesn't slow down the starting time of the program so it doesn't matter that we import the module has a whole, so people don't really care?
Is that correct? And if so, what happens to this very same problem with a module that is poorly written and that would actually slow down the program if imported has a whole?