是否会支持更通用的cmp / __ cmp__ [英] Would there be support for a more general cmp/__cmp__

查看:85
本文介绍了是否会支持更通用的cmp / __ cmp__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果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

推荐答案

Antoon Pardon写道:
Antoon Pardon wrote:
现在的问题是cmp协议无法指示两个对象无法比拟,它们并不相等,但它们都不会比另一个更小或更大。
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.




如果是这种情况,那么你实现__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.


Op 2005-10-20,Duncan Booth schreef< du ********** @ invalid.invalid>:
Op 2005-10-20, Duncan Booth schreef <du**********@invalid.invalid>:
Antoon Pardon写道:
Antoon Pardon wrote:
现在的问题是cmp协议没有
指示两个物体无法比拟的方式,它们并不相等,但它们都不会比另一个更小或更大。
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.



如果是这样,那么你实现__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


Antoon Pardon写道:
Antoon Pardon wrote:
Op 2005-10-20,Duncan Booth schreef< du ********** @ invalid.invalid>:
Op 2005-10-20, Duncan Booth schreef <du**********@invalid.invalid>:
Antoon Pardon写道:
Antoon Pardon wrote:
现在的问题是cmp协议没有办法表明两个对象是无法比拟的,它们并不相等,但两者都不是一个或者大于另一个。
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.



如果是这种情况,那么你实现__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__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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