Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there standard aliases for modules in Python?

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).

like image 614
jeannej Avatar asked Dec 05 '18 21:12

jeannej


People also ask

Does Python support aliases for module names?

Aliasing ModulesIt is possible to modify the names of modules and their functions within Python by using the as keyword.

What is module aliasing in Python?

In Python, aliasing happens whenever one variable's value is assigned to another variable, because variables are just names that store references to values.

How should Python modules be named?

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.

Can we create an alias for module *?

If you want to refer to a module by using a different name, you can create an alias.


1 Answers

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

  1. Google also has a style guide; they suggest using import...as "only when [it's] a standard abbreviation (e.g., np for numpy)". Sorry, they defer too :)
  2. Not to nitpick, but your question states "I am changing all the... [import statements]" (emphasis mine). To be clear, the question you link says "The choice between one or the other then, should be based on coding style instead." There are times when importing a function or class directly is perfectly unambiguous, imo. For example, the 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 unique
  3. This can also be used as a versioning hack. I sometimes use it this way, but I'm not sure how recommended this strategy is
  4. Some people get technical about conversational use of the word "alias"
  5. Reddit doesn't have a list either
  6. To clarify question from the comments, the line between "third party but standard" and "third party" is very informal and not meant to be sacred
like image 148
en_Knight Avatar answered Sep 18 '22 00:09

en_Knight