【发布时间】:2020-05-08 21:30:29
【问题描述】:
我有一个带有浮点 x 属性的 Thing 类。我想大致比较Thing 的两个实例,相对容差为 1e-5。
import attr
@attr.s
class Thing(object):
x: float = attr.ib()
>>> assert Thing(3.141592) == Thing(3.1415926535) # I want this to be true with a relelative tolerance of 1e-5
False
我是否需要重写 __eq__ 方法,或者是否有一种简洁的方式告诉 attr 使用 math.isclose() 或自定义比较函数?
【问题讨论】:
标签: python-attrs