我们如何/为什么使用operator.abs [英] How/why do we use operator.abs
问题描述
在operators
模块中,我们有一个辅助方法operator.abs
.但是在python中,abs
已经是一个函数,而且我知道在对象上调用__abs__
方法的唯一方法是通过函数调用.
In operators
module we have a helper method operator.abs
. But in python abs
is already a function, and the only way I know to invoke the __abs__
method on an object is by function call anyway.
还有其他我不知道采用数字绝对值的奇特方式吗?如果不是,为什么首先存在operator.abs
?在一个示例中,您将不得不使用它来代替普通的abs
?
Is there some other fancy way I don't know about to take the absolute value of a number? If not, why does operator.abs
exist in the first place, and what is an example where you would have to use it instead of plain old abs
?
推荐答案
绝对(...)
abs(a)-与abs(a)相同.
abs(...)
abs(a) -- Same as abs(a).
不,我认为您不知道还有其他花哨的方法,或者除了一致性之外,根本没有任何理由使它完全存在(因为operator
具有用于其他
No, I don’t think there’s some other fancy way you don’t know about, or really any reason at all for this to be here at all except for consistency (since operator
has methods for the other __operator__
s).
这篇关于我们如何/为什么使用operator.abs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!