Python中的模块是否有标准别名

Python中的模块是否有标准别名

本文介绍了Python中的模块是否有标准别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循中提出的准则,我正在更改模块导入功能
函数(agt)

Following the guidelines proposed in this post, I am changing all the

from module import function
function(agt)

方式:

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

scipy.linalg 时间 scipy.io cmath 等?您使用哪个?随意提供其他别名,如果还不存在这样的列表,我愿意提出一个(我将更新这篇文章)。

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

推荐答案

否,没有模块首字母缩略词的完整列表

没有规范的列表,我不建议在此处创建一个列表(因此,这并不是该IMO的真正去处)。 PEP8中为python定义了样式指导,包括命名约定,有关导入的部分为。好的和有用的建议,但不是列举的导入别名列表。

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.

关于命名有很多好的规则和建议,其中一些规则和建议也适用于导入的名称。 例如,我认为这可以使我们理解从算术绘图库到 mpl的不太令人惊讶的词源。按照模块名称上的可以

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.

请注意,您混用了( 时间),带有标准但第三方库( numpy ,这无处不在,但并且必须单独安装)和一般的第三方库( matplotlib )。您可能会 找到内置库和非常常见的库(或通过教程间接获得的)的列表,但是包含第三方库的可能性似乎很小;我建议让软件包的作者决定其标准缩写。 Numpy是一个很好的例子,它的作者在自己的教程中使用 np并已标准化了它们的库使用语法。

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"). 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,我不喜欢称数学为 m(或如一位评论者所言,为 os as o);一个名字的变量对于像我这样笨拙的程序员来说是一个灾难的秘诀...

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

我还要指出 ,这可以消除可能的名称冲突(如果您从 scipy 导入 cos sympy ,您可能会遇到困难)。使用它来缩写已经符合PEP命名标准的名称(开头简短易读),即使方便,听起来也不应该得到官方认可。

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.

在一个有趣的示例中:, sympy scipy 都试图在短时间内使用相同的缩写,(尽管 scipy 现在建议不要使用首字母缩写词完全)

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)

脚注


  1. Google也有风格指南; 使用 import ... as 仅当[它]是标准缩写(例如, numpy np 时) 。抱歉,他们也推迟了:)

  2. 不要挑剔,但是您的问题是我正在更改全部 ... [导入语句](强调我)。明确地说,您链接的问题是:然后在一个或另一个之间进行选择,应基于编码样式。有时直接导入一个函数或类是完全明确的,imo。例如,示例无需进行任何命名间隔或缩写。只有您知道您的应用程序,但是在大多数情况下,假设该名称足够唯一是安全的

  3. 也可以使用。我有时会以这种方式使用它,但是我不确定该策略的推荐程度

  4. 有些人对会话


  5. 为澄清评论中的问题,第三方但标准与第三方是非正式的,并不是神圣的

  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

这篇关于Python中的模块是否有标准别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:40