Should I use np.absolute or np.abs?

It’s likely because there a built-in functions with the same name, abs. The same is true for np.amaxnp.amin and np.round_.

The aliases for the NumPy functions absminmax and round are only defined in the top-level package.

So np.abs and np.absolute are completely identical. It doesn’t matter which one you use.

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

But there are reasons to have different names too: NumPy (or more generally 3rd party packages) sometimes need the Python functions absmin, 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 builtins to access the built-in functions if one shadowed a built-in name.

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.

Leave a Comment