Ich mache ein Projekt in Python (3.2), für das ich benutzerdefinierte Objekte vergleichen muss. Ich bin es gewohnt, in Java OOP, wo man würde eine compareTo()
Methode in der Klasse definieren, die die natürliche Ordnung dieser Klasse gibt an, wie im Beispiel unten:Python entspricht Java's 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;
}
}
Ich bin ziemlich neu in Klassen/Objekte in Python, also würde ich gerne wissen, was ist der "pythonische" Weg, um die natürliche Ordnung einer Klasse zu definieren?
Du meinst, wie [ '__cmp__'] (http : //docs.python.org/reference/datamodel.html#object.__cmp__)? –