python之operator模块

python3中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。

在没有导入模块情况下,会出现

提示找不到cmp函数了,那么在python3中该如何使用这个函数呢?

#内置函数 
operator.lt(a, b) 
operator.le(a, b) 
operator.eq(a, b) 
operator.ne(a, b) 
operator.ge(a, b) 
operator.gt(a, b) 
operator.__lt__(a, b) 
operator.__le__(a, b) 
operator.__eq__(a, b) 
operator.__ne__(a, b) 
operator.__ge__(a, b) 
operator.__gt__(a, b)   

简单说明下这几个函数的意义:

#函数返回值为布尔型
#在返回 true的情况下
lt(a,b)   表示  a<b :从第一个数字或字母(ASCII)比大小 

le(a,b)   表示  a<=b

eq(a,b)   表示  a==b 字母完全一样,返回True,

ne(a,b)   表示  a!=b

gt(a,b)   表示  a>b

ge(a,b)   表示  a>=b
posted @ 2018-03-14 23:40  chinesern  阅读(2481)  评论(0编辑  收藏  举报