Great answers here (I supported them all), and here are my thoughts on this:
First, referring to each of your bullets:
(Presumably) Pros of FMIF:
- Code brevity: shorter function names help stick to 80 columns per row.
Perhaps, but the module names are usually quite short, so that doesn't matter. Of course, there is datetime , but also os , re , sys , etc. And Python has free line breaks inside { [ ( . And for nested modules, always as in IM and FPIM
- Readability: chisquare (...) looks more readable than scipy.stats.stats.chisquare (...).
I absolutely disagree. When reading external code (or my own code after a few months), it’s hard to know where each function comes from. Qualified names do not allow me to go from line 2345 to the module declaration header. And it also gives you context: " chisquare ? What is it? Oh, is it from scypy ? Well, some math-related stuff then." And again, you can always abbreviate scipy.stats.stats as scypyst . scypyst.chisquare(...) is short enough with all the advantages of a qualified name.
import os.path as osp is another good example, given that it very often combines 3 or more of its functions together in one call: join (expanduser (), basename (splitext ()), etc.
- Ease of redirection: one-line redefinition of a function from altmodule instead of a module.
How often do you want to override one function, but not the entire module? The boundaries of the modules and the coordination of functions must be preserved, and Alex has already explained this to great depths. For most (all?) Real-world alt_module.x , if alt_module.x is a viable replacement for module.x , then alt_module itself is probably an alternative for module , so both IM and FPIM are single-line like FMIF if you use as .
- I understand that FPIM somehow negates the first two problems ...
In fact, as is one that mitigates the first 2 issues (and 3rd), not FPIM. You can also use IM for this: import some.long.package.path.x as x for the same result as FPIM.
Thus, none of the above factors are FMIF pros. And the reasons why I prefer IM / FPIM are as follows:
For simplicity and consistency, when I import something, IM or FPIM, I always import a module, not an object from a module. Remember that FMIF can be (ab-) used to import functions, classes, variables, or even other modules! Think about the clutter from somemodule import sys, somevar, os, SomeClass, datetime, someFunc .
In addition, if you want more than one object from a module, FMIF will pollute your namespace more than IM or FPIM, which will use the same name no matter how many objects you want to use. And such objects will have a qualified name, which is pro, not con: as I said in release 2, IMHO a improves readability.
it all comes down to consistency, simplicity, organization. “Importing Modules, Not Objects” is a good, easy model of the mind.