问题描述
我想知道如果cmp函数和
__cmp__方法会更加普遍,人们会有怎样的感觉。
现在的问题是cmp协议没办法
表示两个对象是无法比拟的,它们不是等于b
等于但是它们都不比另一个更小或更大。
所以我认为cmp可以在这个
的情况下返回None或抛出一个特定的异常。写一个
__cmp__方法的人也可以这样做。
-
Antoon Pardon
I was wondering how people would feel if the cmp function and
the __cmp__ method would be a bit more generalised.
The problem now is that the cmp protocol has no way to
indicate two objects are incomparable, they are not
equal but neither is one less or greater than the other.
So I thought that either cmp could return None in this
case or throw a specific exception. People writing a
__cmp__ method could do the same.
--
Antoon Pardon
推荐答案
如果是这种情况,那么你实现__eq__和__ne__来返回
True / False并使cmp抛出异常。我认为没有必要延长cmp协议。
If that is the case then you implement __eq__ and __ne__ to return
True/False and make cmp throw an exception. I don''t see any need to extend
the cmp protocol.
如果是这样,那么你实现__eq__和__ne__返回
True / False并使cmp抛出异常。我认为没有必要扩展cmp协议。
If that is the case then you implement __eq__ and __ne__ to return
True/False and make cmp throw an exception. I don''t see any need to extend
the cmp protocol.
这还不够。存在订单存在的域,但不是总订单的
。所以对于一些元素来说,可以说
一个比另一个少,但不是所有这样的夫妻。
你的解决方案不会''允许这样的情况。
-
Antoon Pardon
That is not sufficient. There are domains where an order exists, but not
a total order. So for some couples of elements it is possible to say
one is less than the other, but not for all such couples.
Your solution wouldn''t allow for such a case.
--
Antoon Pardon
如果是这种情况,那么你实现__eq__和__ne__来返回
True / False并使cmp抛出异常。我认为没有必要扩展cmp协议。
If that is the case then you implement __eq__ and __ne__ to return
True/False and make cmp throw an exception. I don''t see any need to
extend the cmp protocol.
这还不够。存在订单存在的域,但不是总订单。因此,对于一些元素的元素,可以说一个元素比另一个元素小,但不是所有这样的元素。
你的解决方案不允许这样的情况。
That is not sufficient. There are domains where an order exists, but
not a total order. So for some couples of elements it is possible to
say one is less than the other, but not for all such couples.
Your solution wouldn''t allow for such a case.
我不明白为什么不。如果存在排序,则__cmp__返回结果
否则会抛出异常。只要定义__eq__和__ne__,
__cmp__在检查相等性时从不被调用。
I don''t see why not. If an ordering exists then __cmp__ returns a result
otherwise it throws an exception. So long as you define __eq__ and __ne__,
__cmp__ is never called when checking for equality.
这篇关于是否会支持更通用的cmp / __ cmp__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!