ast*_*y13 13 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我对Python中的类/对象相当新,所以我想知道定义类的自然顺序的"pythonic"方法是什么?
pok*_*oke 13
你可以实现一个特殊的方法__lt__,__gt__等来实现默认的运营商定制的类型.在语言参考中查看有关它们的更多信息.
例如:
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)
Run Code Online (Sandbox Code Playgroud)
或者如评论中的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
Run Code Online (Sandbox Code Playgroud)
Python有类似的功能:__cmp__().
我现在看到你问的是Python 3. 他们的"最新消息"表明:
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))
Run Code Online (Sandbox Code Playgroud)
要么
@classmethod
def compare(cls, a, b):
return ((a > b) - (a < b))
Run Code Online (Sandbox Code Playgroud)
实施后__gt__()和__lt__().
您将使用哪个:
f1 = Foo(1,1)
f2 = Foo(2,2)
f1.compareTo(f2)
Foo.compare(f1,f2)
Run Code Online (Sandbox Code Playgroud)
这将为您提供相同的功能.