Following the guidelines proposed in this post, I am changing all the
from module import function
function(agt)
by:
import module as mdl
mdl.function(agt)
in my codes. I am trying to use commonly used aliases rather than personal ones. Is there a list of some kind on the internet summing-up all well-used aliases ?
For instance, these appear to be pretty common:
import numpy as np
import math as m
import matplotlib.pyplot as plt
What about aliases for scipy.linalg
, time
, scipy.io
, cmath
and so on ? Which do you use ? Feel free to give other aliases, if no such list exist yet, I am willing to propose one (I will update this post).
Aliasing ModulesIt is possible to modify the names of modules and their functions within Python by using the as keyword.
In Python, aliasing happens whenever one variable's value is assigned to another variable, because variables are just names that store references to values.
Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
If you want to refer to a module by using a different name, you can create an alias.
No, there is no complete list of module acronyms
There is no canonical list, and I wouldn't advise making one here (SO isn't really the place for that IMO). Style guidlines, including naming conventions are defined in PEP8 for python and the section on importing is here. Good and helpful advice, but not an enumerated list of import aliases.
There are a lot of good rules and advice on naming, some of which applies to imported names too. This post, for example, I think, can get us at the not-so-surprising etymology of "Math Plotting Library" to "mpl". Following the PEP8 on module names can help us not need acronyms at all.
Note that you've mixed built-in modules ("time
") with standard but third-party libraries ("numpy
", which is ubiquitous but Guido declined in 2006 to add to the core and must be installed separately) and general third-party libraries ("matplotlib
")6. You might find a list for built-in and extremely common libraries (or an indirect one through tutorials), but third-party libraries being included seems far less likely; I would advise letting the authors of packages decide their standard abbreviations. Numpy is a good example of a library who's authors use "np" in their own tutorials and have "standardized" their libraries usage syntax.
FWIW, I don't like calling math "m" (or as one commenter suggests, "os as o"); one-name variables are a recipe for disaster for clumsy programmers like me...
I'd also point you to the rationale for including the "as" syntax in the first place, which justifies it by eliminating possible name clashes (if you import cos
from scipy
and from sympy
, you might have a bad time). Using it to abbreviate names that adhere to the PEP naming standard already (are short and readable, to start), doesn't sound like it should be officially endorsed even if convenient.
In an amusing example: here, sympy
and scipy
both tried to use the same abbreviation for a short while, causing the exact problem we hoped to avoid (although scipy
now advises not using an acronym at all, evidently)
Footnotes
import...as
"only when [it's] a standard abbreviation (e.g., np
for numpy
)". Sorry, they defer too :)sklearn
tutorial example that includes an import of "GaussianProcessRegressor" doesn't need to do any namespacing or abbreviations. Only you can know for your application, but it's safe in most cases to assume that that name is sufficiently uniqueIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With