resoult在python啥意思,python - __lt__而不是__cmp__

python - __lt__而不是__cmp__

Python 2.x有两种方法可以重载比较运算符,__cmp__或者#34;丰富的比较运算符" 例如__cmp__。据说富裕的比较超载是首选,但为什么会这样呢?

丰富的比较运算符更容易实现每个,但您必须使用几乎相同的逻辑实现其中几个。 但是,如果你可以使用内置的__cmp__和元组排序,那么<=变得非常简单并完成所有的比较:

class A(object):

def __init__(self, name, age, other):

self.name = name

self.age = age

self.other = other

def __cmp__(self, other):

assert isinstance(other, A) # assumption for this example

return cmp((self.name, self.age, self.other),

(other.name, other.age, other.other))

这种简单性似乎比重载所有6(!)丰富的比较更好地满足了我的需求。 (但是,如果依靠&#34;交换的参数&#34; /反映的行为,你可以把它归结为&#34; 4,但这导致并发症的净增加,在我的拙见中。)

如果我只是超载__cmp__,是否有任何不可预见的陷阱需要注意?

我理解__cmp__,<=,==等操作符可以为其他目的而重载,并且可以返回他们喜欢的任何对象。 我并不是在询问这种方法的优点,而是仅仅考虑使用这些算子进行比较时的差异,这与他们对数字的意义相同。

更新:正如克里斯托弗指出的那样,__cmp__正在消失3.x. 是否有任何替代方案可以使实施比较像上面的__cmp__一样简单?

5个解决方案

84 votes

是的,很容易实现所有内容,例如: __key__有一个mixin类(或者一个元类,或者一个类装饰器,如果你的味道以这种方式运行)。

例如:

class ComparableMixin:

def __eq__(self, other):

return not self

def __ne__(self, other):

return self

def __gt__(self, other):

return other

def __ge__(self, other):

return not self

def __le__(self, other):

return not other

现在你的类只能定义__key__并从ComparableMixin中多次继承(在需要其他任何基础之后,如果有的话)。 类装饰器非常相似,只是插入类似的函数作为它所装饰的新类的属性(结果可能在运行时在显微镜下更快,在内存方面同样是微小的成本)。

当然,如果你的班级有一些特别快速的实施方式(例如)__key__和__cmp__,它应该直接定义它们以便不使用mixin的版本(例如,__lt__的情况) - 在 事实__ne__可能被定义为促进这一点:

def __ne__(self, other):

return not self == other

但在上面的代码中,我想保持令人愉悦的对称性,只使用__key__ ;-)。至于为什么__cmp__必须去,因为我们确实有__lt__和朋友,为什么要保持另一种不同的方式来做同样的事情呢? 它在每个Python运行时(Classic,Jython,IronPython,PyPy,......)中都非常重要。 肯定不会有错误的代码就是那些不存在的代码 - 而Python的原则应该是理想情况下执行任务的一种明显方式(C具有相同的原则) &#34; ISO标准的C&#34;部分,btw)。

这并不意味着我们会竭尽全力禁止某些事情(例如,某些用途的mixins和类装饰器之间的近似等价),但这肯定意味着我们不想随身携带代码 冗余存在的编译器和/或运行时只是为了支持多个等效方法来执行完全相同的任务。

进一步编辑:实际上有一种更好的方法可以为许多类提供比较和散列,包括问题中的方法 - 一种__key__方法,正如我在对问题的评论中提到的那样。 因为我从来没有为它编写PEP,所以如果你喜欢的话,你现在必须使用Mixin(&amp; c)来实现它:

class KeyedMixin:

def __lt__(self, other):

return self.__key__() < other.__key__()

# and so on for other comparators, as above, plus:

def __hash__(self):

return hash(self.__key__())

对于实例与其他实例的比较来说,将每个元组的元组与几个字段进行比较,这是一个非常常见的情况 - 然后,散列应该在完全相同的基础上实现。 __key__特殊方法直接解决了这个问题。

Alex Martelli answered 2019-08-14T04:12:29Z

44 votes

为了简化这种情况,Python 2.7 + / 3.2 +中的类装饰器,functools.total_ordering,可用于实现Alex建议的内容。 来自文档的示例:

@total_ordering

class Student:

def __eq__(self, other):

return ((self.lastname.lower(), self.firstname.lower()) ==

(other.lastname.lower(), other.firstname.lower()))

def __lt__(self, other):

return ((self.lastname.lower(), self.firstname.lower()) <

(other.lastname.lower(), other.firstname.lower()))

jmagnusson answered 2019-08-14T04:12:55Z

9 votes

这由PEP 207 - Rich Comparisons涵盖

此外,__cmp__在python 3.0中消失了。 (请注意,它不在[http://docs.python.org/3.0/reference/datamodel.html]上,但它位于[http://docs.python.org/2.7/reference/datamodel.html])

Christopher answered 2019-08-14T04:13:29Z

0 votes

灵感来自Alex Martelli的ComparableMixin&amp; KeyedMixin答案,我想出了以下mixin。它允许您实现单个_compare_to()方法,该方法使用基于密钥的比较类似于KeyedMixin,但允许您的类根据other的类型选择最有效的比较密钥。(请注意,这个mixin对于可以测试相等而不是顺序的对象没有多大帮助)。

class ComparableMixin(object):

"""mixin which implements rich comparison operators in terms of a single _compare_to() helper"""

def _compare_to(self, other):

"""return keys to compare self to other.

if self and other are comparable, this function

should return ``(self key, other key)``.

if they aren't, it should return ``None`` instead.

"""

raise NotImplementedError("_compare_to() must be implemented by subclass")

def __eq__(self, other):

keys = self._compare_to(other)

return keys[0] == keys[1] if keys else NotImplemented

def __ne__(self, other):

return not self == other

def __lt__(self, other):

keys = self._compare_to(other)

return keys[0] < keys[1] if keys else NotImplemented

def __le__(self, other):

keys = self._compare_to(other)

return keys[0] <= keys[1] if keys else NotImplemented

def __gt__(self, other):

keys = self._compare_to(other)

return keys[0] > keys[1] if keys else NotImplemented

def __ge__(self, other):

keys = self._compare_to(other)

return keys[0] >= keys[1] if keys else NotImplemented

Eli Collins answered 2019-08-14T04:13:58Z

0 votes

(17/17/17编辑,以考虑评论。)

我在上面尝试了类似的mixin答案。 我和#34;无&#34;遇到了麻烦。 这是一个修改版本,处理与&#34;无&#34;的相等比较。 (我没有理由因为缺乏语义而无法与None进行不等式比较):

class ComparableMixin(object):

def __eq__(self, other):

if not isinstance(other, type(self)):

return NotImplemented

else:

return not self

def __ne__(self, other):

return not __eq__(self, other)

def __gt__(self, other):

if not isinstance(other, type(self)):

return NotImplemented

else:

return other

def __ge__(self, other):

if not isinstance(other, type(self)):

return NotImplemented

else:

return not self

def __le__(self, other):

if not isinstance(other, type(self)):

return NotImplemented

else:

return not other

Gabriel Ferrer answered 2019-08-14T04:14:32Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值