Why do scientists bother with the np
for numpy
?
(Note this is more a "philosophical"/reasoning question - let us assume that you get your code to perform as intended regardless of choice of syntax)
When I write code for scientific applications, mathematical functions such as sqrt
, as well as arrays and the many other features of Numpy are "bread and butter" - ubiquitous and taken for granted. For this reason, I always use
from numpy import *
instead of
import numpy as np
despite nearly every online example I see. Indeed, a colleague of mine who found my code useful said that they had to go through it and insert np
everywhere. And yet, why bother? I can honestly say that in the thousands of lines of Python I've written, those three characters would have been redundant and a waste of my time. And that time is not trivial: as stated in many SE discussions, programmer time is more valuable than CPU time. If you want a super optimized program, use C
or Fortran
for HPC instead of python
.
Using
from numpy import sqrt
is also an option, but also has drawbacks for rapidly making a script where you might want to use a new function out of the blue (as you would in C
, Fortran
, Matlab
, whatever).
Suppose that there is a library for accurately calculating the square root of a complex number - then it's still faster to import complxlib as cplx
, use cplx.sqrt
when necessary and sqrt
the other 99% of the time. So is there a real example or argument as to why my approach is bad practice?
Please note that the question is specifically about numpy
- a staple in scientific application of python, rather than e.g. super_esoteric_library8472
.
std::cout
. If I seenp.sqrt
I know without error the exact function and there is not conflict also in future. $\endgroup$from scipy import *; from numpy import *; log(1j)
— 200 lines of code below you will wonder why it doesn't work. $\endgroup$using namespace std
in C++ code by default - for a standard C++ library. Why should the same controversial exclusive approach apply to a non-standard (but of course very popular) packagenumpy
? $\endgroup$from numpy import sqrt as s
. $\endgroup$