相关疑难解决方法(0)

有没有办法在Python中返回min和max的自定义值?

我有一个自定义类,

class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b
Run Code Online (Sandbox Code Playgroud)

该类不可迭代或可索引或类似的东西.如果可能的话,我想保持这种方式.有可能像以下工作一样吗?

>>> x = A(1, 2)
>>> min(x)
1
>>> max(x)
2
Run Code Online (Sandbox Code Playgroud)

让我想到这一点的是,minmax文档中列为"通用序列操作" .由于range被同一个文档认为是序列类型,我认为必须有某种可能的优化range,也许我可以利用它.

也许有一种我不知道的神奇方法可以实现这一点吗?

python magic-methods python-3.x

17
推荐指数
3
解决办法
3096
查看次数

标签 统计

magic-methods ×1

python ×1

python-3.x ×1