python调用compare工具,Python相当于Java的compareTo()

I'm doing a project in Python (3.2) for which I need to compare user defined objects. I'm used to OOP in Java, where one would define a compareTo() method in the class that specifies the natural ordering of that class, as in the example below:

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;

}

}

I'm fairly new to classes/objects in Python, so I'd like to know what is the "pythonic" way to define the natural ordering of a class?

解决方案

You can implement the special methods __lt__, __gt__ etc. to implement the default operators for custom types. See more about them in the language reference.

For example:

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)

Or as said by stranac in the comments, you can use the total_ordering decorator to save some typing:

@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

### 字符串比较方法 `s1.compare(s2)` 的用法与实现 在编程中,字符串是比较常见的数据结构之一。许多编程语言提供了内置的方法来执行字符串之间的比较操作。以下是关于 `compare` 方法的一些常见特性和实现细节。 #### 1. **C++ 中的 `std::string::compare`** 在 C++ 中,标准库中的 `std::string` 类提供了一个名为 `compare` 的成员函数用于比较两个字符串的内容。该方法会逐字符对比两个字符串,并基于字典顺序返回一个整数值: - 如果返回值小于零,则表示调用对象(即 `s1`)在字典序上小于参数字符串(即 `s2`)。 - 如果返回值等于零,则说明两字符串相等。 - 如果返回值大于零,则表明调用对象在字典序上大于参数字符串[^1]。 下面是一个简单的例子展示如何使用此功能: ```cpp #include <iostream> #include <string> int main() { std::string s1 = "apple"; std::string s2 = "banana"; int result = s1.compare(s2); if (result < 0) { std::cout << "\"" << s1 << "\" is less than \"" << s2 << "\"." << std::endl; } else if (result == 0) { std::cout << "\"" << s1 << "\" is equal to \"" << s2 << "\"." << std::endl; } else { std::cout << "\"" << s1 << "\" is greater than \"" << s2 << "\"." << std::endl; } return 0; } ``` #### 2. **Java 中的 `String.compareTo`** 类似的,在 Java 编程语言里也有相应的机制来进行这种类型的比较——通过 `compareTo()` 函数完成相同的功能。它同样遵循上述规则并依据 ASCII 值判断大小关系: ```java public class Main { public static void main(String[] args) { String str1 = new String("hello"); String str2 = new String("world"); System.out.println(str1.compareTo(str2)); // 输出负数因为'h'<'w' } } ``` 值得注意的是,尽管这两个例子都涉及到了基本概念上的相似之处,但具体语法以及内部处理方式可能因不同平台而有所差异。 #### 3. **Python 中无显式的 Compare Method** 相比之下,像 Python 这样的高级脚本语言并没有专门定义类似于其他低级或中级面向对象程序设计语言那样的独立 compare 方法;但是可以通过直接利用运算符(`<`, `>`, `==`)达成同样的目的。这些操作实际上背后也是按照字母表顺序依次检查每一个对应位置上的字符直至找到第一个不同的地方为止. 例如: ```python str_a = 'abcde' str_b = 'ABCDE' if str_a.lower() < str_b.lower(): print('First string comes before second.') elif str_a.lower() > str_b.lower(): print('Second string comes after first.') else: print('Both strings are identical when case ignored.') ``` 这里我们还额外展示了忽略大小写的版本以便更贴近实际应用场景需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值