6.4 笔记

Python 3.X 的版本中已经没有 cmp 函数,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象,包含的方法有:

 1 operator.lt(a, b)
 2 operator.le(a, b)
 3 operator.eq(a, b)
 4 operator.ne(a, b)
 5 operator.ge(a, b)
 6 operator.gt(a, b)
 7 operator.__lt__(a, b)
 8 operator.__le__(a, b)
 9 operator.__eq__(a, b)
10 operator.__ne__(a, b)
11 operator.__ge__(a, b)
12 operator.__gt__(a, b)

 

 

实例

1 >>> import operator
2 >>> operator.eq('hello', 'name');
3 False
4 >>> operator.eq('hello', 'hello');
5 True

 

 
posted @ 2022-06-04 12:59  闻淤野  阅读(36)  评论(0)    收藏  举报