使用重载的比较运算符从int派生类访问原始int比较

时间:2016-11-28 14:05:15

标签: python python-2.7 inheritance operator-overloading comparison-operators

我有一个带有重载比较运算符的int派生类。

在重载方法的主体中,我需要使用原始运算符。

玩具示例:

>>> class Derived(int):
...     def __eq__(self, other):
...         return super(Derived, self).__eq__(other)

在Python 3.3+中运行良好,但在Python 2.7中失败,异常为AttributeError: 'super' object has no attribute '__eq__'

我可以考虑几个walkarrounds,我觉得不是很干净:

return int(self) == other

需要创建一个新的int对象,只是为了比较它,而

try:
    return super(Derived, self).__eq__(other)
except AttributeError:
    return super(Derived, self).__cmp__(other) == 0

基于Python版本拆分控制流,我发现它非常混乱(因此明确地检查Python版本)。

如何使用Python 2.7和3.3 +以优雅的方式访问原始整数比较?

4 个答案:

答案 0 :(得分:2)

Python 2和3之间存在显着差异,所以我认为你应该咬紧牙关并检查版本。只有在您尝试编写适用于这两种情况的代码时才会出现这种情况(根据我的经验,您会发现必须修补的内容)。为避免任何性能影响,您可以执行以下操作:

from six import PY2

class Derived(int):
    if PY2:
        def __eq__(self, other):
            return super(Derived, self).__cmp__(other) == 0
    else:
        def __eq__(self, other):
            return super(Derived, self).__eq__(other)

这就是我要做的。如果我真的想要继承int ......

如果你真的不想,也许你可以试试:

class Derived(int):
    def __eq__(self, other):
        return (self ^ other) == 0

显然,如果你关心性能,你必须对其余的代码进行一些分析,并找出它们中的任何一个是否明显更糟......

答案 1 :(得分:1)

两个版本都实现了__xor__方法,你可以试试这个:

class Derived(int):
    def __eq__(self, other):
        return not super(Derived, self).__xor__(other)

答案 2 :(得分:0)

我认为您应该在定义课程之前在IEnumerable<Orgs> orgsModel = GetFromDatabaseOrWhateverYourBackingStoreIs(); return orgsModel.GetRegionBuildingIds( forRegionId: 123 ); 中定义__eq__。例如:

int

这应该为int = 5 def int.__eq__(self, other): return self.real == other IntDerived = Derived(int) 类提供super属性。

EDITED

主要思想奏效了,但我注意到代码不起作用。所以:改进代码:

__eq__

答案 3 :(得分:0)

使用hasattr可以避免创建新的int对象,捕获异常或显式检查Python版本。

以下代码适用于Python 2.7和3.3 +:

class Derived(int):
    def __eq__(self, other):
        return super(Derived, self).__cmp__(other) == 0 if hasattr(Derived, "__cmp__") else super(Derived, self).__eq__(other)
相关问题