Python相当于Java的compareTo()

时间:2012-06-26 21:01:00

标签: python oop class object

我正在用Python(3.2)做一个项目,我需要比较用户定义的对象。我习惯于Java中的OOP,在类中定义一个compareTo()方法,指定该类的自然顺序,如下例所示:

public class Foo {
    int a, b;

    public Foo(int aa, int bb) {
        a = aa;
        b = bb;
    }

    public int compareTo(Foo that) {
        // return a negative number if this < that
        // return 0 if this == that
        // return a positive number if this > that

        if (this.a == that.a) return this.b - that.b;
        else return this.a - that.a;
    }
}

我对Python中的类/对象相当新,所以我想知道定义类的自然顺序的“pythonic”方法是什么?

2 个答案:

答案 0 :(得分:11)

您可以实现特殊方法__lt____gt__等来实现自定义类型的默认运算符。在language reference中了解有关它们的更多信息。

例如:

class Foo:
    def __init__ (self, a, b):
        self.a = a
        self.b = b

    def __lt__ (self, other):
        if self.a == other.a:
            return self.b < other.b
        return self.a < other.b

    def __gt__ (self, other):
        return other.__lt__(self)

    def __eq__ (self, other):
        return self.a == other.b and self.b == other.b

    def __ne__ (self, other):
        return not self.__eq__(other)

或者如评论中的stranac所说,您可以使用total_ordering装饰器来保存一些输入:

@functools.total_ordering
class Foo:
    def __init__ (self, a, b):
        self.a = a
        self.b = b

    def __lt__ (self, other):
        if self.a == other.a:
            return self.b < other.b
        return self.a < other.b

    def __eq__ (self, other):
        return self.a == other.b and self.b == other.b

答案 1 :(得分:6)

Python具有类似的功能:__cmp__()

我现在看到你在问Python 3. Their "whats new" suggests

The cmp() function should be treated as gone, and the __cmp__() special method 
is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and 
other rich comparisons as needed. (If you really need the cmp() functionality, 
you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)

所以看起来你总是可以做像

这样的事情
def compareTo(self, that):
    return ((self > that) - (self < that))

@classmethod
def compare(cls, a, b):
    return ((a > b) - (a < b))

在实施__gt__()__lt__()

之后

然后你将使用它:

f1 = Foo(1,1)
f2 = Foo(2,2)

f1.compareTo(f2)
Foo.compare(f1,f2)

这将为您提供同等的功能。