博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python rich comparisons 自定义对象比较过程和返回值
阅读量:6608 次
发布时间:2019-06-24

本文共 1251 字,大约阅读时间需要 4 分钟。

Classes wishing to support the rich comparison mechanisms must add    one or more of the following new special methods:             def __lt__(self, other):                ...             def __le__(self, other):                ...             def __gt__(self, other):                ...             def __ge__(self, other):                ...             def __eq__(self, other):                ...             def __ne__(self, other):                ...    Each of these is called when the class instance is the on the    left-hand-side of the corresponding operators (<, <=, >, >=, ==,    and != or <>). The argument other is set to the object on the    right side of the operator. The return value of these methods is    up to the class implementor (after all, that's the entire point of    the proposal).

来自:

import operatorclass iint(int):    def __init__(self,n):        int.__init__(n)        self.v=n    def __lt__(self,otr):        if operator.lt(self.v,otr):            print '%s is less than %s' %(self.v,otr)            return 10000        print '%s is not less than %s' %(self.v,otr)        return -10000x=iint(1)y=iint(2)print x

结果:

>>> 1 is less than 2100002 is not less than 1-10000

 

转载于:https://www.cnblogs.com/xiangnan/p/3397694.html

你可能感兴趣的文章
我的友情链接
查看>>
Linux压力测试
查看>>
JAVA中的线程机制(二)
查看>>
nginx安装与配置2(转载)
查看>>
沈阳一饭店凌晨爆燃,燃气报警器时刻预防
查看>>
Redis 与 数据库处理数据的两种模式
查看>>
VUE2中axios的使用方法
查看>>
CS 229 notes Supervised Learning
查看>>
2018.10.27-dtoj-3996-Lesson5!(johnny)
查看>>
DataTable转换成json字符串
查看>>
ubuntu 12.04 安装 redis
查看>>
【DM642】ICELL Interface—Cells as Algorithm Containers
查看>>
力扣算法题—085最大矩阵
查看>>
svs 在创建的时候 上传文件夹 bin obj 这些不要提交
查看>>
Tinkphp
查看>>
How to temporally disable IDE tools (load manually)
查看>>
Vue.js学习 Item4 -- 数据双向绑定
查看>>
几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)
查看>>
图片存储类型的种类、特点、区别
查看>>
GETTING UP AND RUNNING WITH NODE.JS, EXPRESS, JADE, AND MONGODB
查看>>