我们如何/为什么使用operator.abs [英] How/why do we use operator.abs

查看:95
本文介绍了我们如何/为什么使用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具有用于其他 s).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆