Let from module import function
be called the FMIF coding style.
Let import module
be called the IM coding style.
Let from package import module
be called the FPIM coding style.
Why is IM+FPIM considered a better coding style than FMIF? (See this post for the inspiration for this question.)
Here are some criteria which lead me to prefer FMIF over IM:
- Shortness of code: It allows me to use shorter function names and thus help stick to the 80 columns-per-line convention.
- Readability:
chisquare(...)
appears more readable thanscipy.stats.stats.chisquare(...)
. Although this is a subjective criterion, I think most people would agree. - Ease of redirection: If I use FMIF and for some reason at some later time want to redirect python to define
function
fromalt_module
instead ofmodule
I need to change just one line:from alt_module import function
. If I were to use IM, I'd need to change many lines of code.
I am interested in all reasons why IM+FPIM may be better than FMIF, but in particular, I'd be interested in elaboration on the following points mentioned here:
Pros for IM:
- ease of mocking/injecting in tests. (I am not very familiar with mocking, though I recently learned what the term means. Can you show code which demonstrates how IM is better than FMIF here?)
- ability for a module to change flexibly by redefining some entries. (I must be misunderstanding something, because this seems to be an advantage of FMIF over IM. See my third reason in favor of FMIF above.)
- predictable and controllable behavior on serialization and recovery of your data. (I really don't understand how the choice of IM or FMIF affects this issue. Please elaborate.)
- I understand that FMIF "pollutes my namespace", but beyond being a negative-sounding phrase, I don't appreciate how this hurts the code in any concrete way.
Many thanks.