Python 我应该使用 np.absolute 还是 np.abs?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45413909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:59:35  来源:igfitidea点击:

Should I use np.absolute or np.abs?

pythonnumpyabsolute-value

提问by Jonas Adler

Numpy provides both np.absoluteand the alias np.absdefined via

Numpy 提供了定义np.absolute的别名和别名np.abs

from .numeric import absolute as abs

which seems to be in obvious violation of the zen of python:

这似乎明显违反了python的禅宗

There should be one-- and preferably only one --obvious way to do it.

应该有一种——最好只有一种——明显的方法来做到这一点。

So I'm guessing that there is a good reason for this.

所以我猜这是有充分理由的。

I have personally been using np.absin almost all of my code and looking at e.g. the number of search results for np.absvs np.absoluteon Stack Overflow it seems like an overwhelming majority does the same (2130 vs 244 hits).

我个人np.abs几乎在我所有的代码中都使用过,并查看例如Stack Overflow 上np.absnp.absolute的搜索结果数量,似乎绝大多数人都这样做(2130 对 244 次点击)。

Is there any reason i should preferentially use np.absoluteover np.absin my code, or should I simply go for the more "standard" np.abs?

有什么理由我应该优先在我的代码中使用np.absoluteover np.abs,还是应该简单地选择更“标准”的np.abs

采纳答案by MSeifert

It's likely because there a built-in functions with the same name, abs. The same is true for np.amax, np.aminand np.round_.

这可能是因为有一个同名的内置函数,abs. 对于np.amax,np.amin和也是如此np.round_

The aliases for the NumPy functions abs, min, maxand roundare only defined in the top-level package.

为NumPy的函数的别名absminmaxround仅在顶层包定义。

So np.absand np.absoluteare completely identical. It doesn't matter which one you use.

所以np.absnp.absolute完全相同。你使用哪一种并不重要。

There are several advantages to the short names: They are shorter and they are known to Python programmers because the names are identical to the built-in Python functions. So end-users have it easier (less to type, less to remember).

短名称有几个优点:它们更短,Python 程序员知道它们,因为它们的名称与内置的 Python 函数相同。所以最终用户更容易(更少打字,更少记住)。

But there are reasons to have different names too: NumPy (or more generally 3rd party packages) sometimes need the Python functions abs, min, etc. So inside the package they define functions with a different name so you can still access the Python functions - and just in the top-level of the package you expose the "shortcuts". Note: Different names are not the only available option in that case: One could work around that with the Python module builtinsto access the built-in functions if one shadowed a built-in name.

但也有原因,有不同的名称太:NumPy的(或更一般的第三方包)有时需要Python的功能absmin等于是他们定义使用不同的名称功能包里面,所以你仍然可以访问Python的功能-和公正在包的顶层,您公开了“快捷方式”。注意:在这种情况下,不同的名称并不是唯一可用的选项:builtins如果隐藏了内置名称,则可以使用 Python 模块解决该问题以访问内置函数。

It might also be the case (but that's pure speculation on my part) that they originally only included the long-named functions absolute(and so on) and only added the short aliases later. Being a large and well-used library the NumPy developers don't remove or deprecate stuff lightly. So they may just keep the long names around because it could break old code/scripts if they would remove them.

也可能是这种情况(但这纯粹是我的猜测)它们最初只包含长命名的函数absolute(等等),后来才添加了短别名。作为一个大型且使用良好的库,NumPy 开发人员不会轻易删除或弃用某些东西。因此,他们可能只保留长名称,因为如果删除它们,可能会破坏旧代码/脚本。