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).

来自:http://www.python.org/dev/peps/pep-0207/

import operator

class 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 -10000 x=iint(1)
y=iint(2)
print x<y
print y<x

结果:

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

Python rich comparisons 自定义对象比较过程和返回值的更多相关文章

  1. Android JNI 自定义对象为参数和返回值

    ndroid JNI 提供了很强大的支持,不仅可以采用基本类型做为参数和返回值,同时也支持自定义对象做为参数和返回值,以下举例说明. 一.定义作为输入和返回的自定义类 (仅提供两个简单类型和一个打印函 ...

  2. Android开发 AIDL使用自定义对象作参数或返回值

    http://www.pocketdigi.com/20121129/952.html 默认,AIDL支持对象作参数,但需要该对象实现Parcelable接口,且aidl文件应该是该类在同一包下,需要 ...

  3. Python进阶:自定义对象实现切片功能

    2018-12-31 更新声明:切片系列文章本是分三篇写成,现已合并成一篇.合并后,修正了一些严重的错误(如自定义序列切片的部分),还对行文结构与章节衔接做了大量改动.原系列的单篇就不删除了,毕竟也是 ...

  4. python基础之函数参数、嵌套、返回值、对象、命名空间和作用域

    函数的使用原则 函数的使用必须遵循:先定义后使用的原则 函数的定义,与变量的定义是相似的,如果没有事先定义函数而直接引用就相当于在引用一个不存在变量名 定义阶段:只检测语法,不执行代码,当出现语法错误 ...

  5. python笔记六(函数的参数、返回值)

    一 调用函数 在写函数之前,我们先尝试调用现有的函数 >>> abs(-9) 9 除此之外,还有我们之前使用的len()等.可以用于数据类型转换的 int() float() str ...

  6. webservice接口测试wsdl,参数是xml格式。python,入参转化成str,返回值转化成dict调用

    1.用SoapUI测试webservice接口,传入参数是xml格式时.xml格式需要将xml的外围增加<![CDATA[xml]]> 2.但是用python去做webservice测试, ...

  7. python -- 初始函数 函数的定义,函数的返回值以及函数的参数

    1.什么是函数,函数的定义及语法 2.函数的返回值 3.函数的参数 一.函数的定义及语法 函数的定义:定义了一个动作或者功能,是对功能的封装 语法: def 函数名( 形参列表 ):          ...

  8. python开发初识函数:函数定义,返回值,参数

    一,函数的定义 1,函数mylen叫做函数名 #函数名 #必须由字母下划线数字组成,不能是关键字,不能是数字开头 #函数名还是要有一定的意义能够简单说明函数的功能 2,def是关键字 (define) ...

  9. 04 python学习笔记-函数、函数参数和返回值(四)

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print(),我们也可以自己创建函数,这叫做用户自定 ...

随机推荐

  1. 前端之旅HTML与CSS篇之block与inline的区别

    display:block;和display:inline;的区别block元素特点: 1)处于常规流中时,如果width没有设置,会自动填充满父容器 2)可以应用margin/padding 3)在 ...

  2. phantomjs 开发爬虫框架

    函数 page.childframescount page.childframesname page.close page.currentframename page.deletelater page ...

  3. leetcode 561.Array Partition I-easy

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  4. [LeetCode] Number of Atoms 原子的个数

    Given a chemical formula (given as a string), return the count of each atom. An atomic element alway ...

  5. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  6. shell编程-邮件发送设置

    在linux 运维过程中,经常会写一些脚本监控一些服务器的状态,如监控redis 主从切换,redis 宕机等,当事件发生时,应该发送邮件通知到相对应的管理员,因此就需要搭建邮件服务,使linux 能 ...

  7. [COGS 2877]老m凯的疑惑

    Description Margatroid退役之后沉迷文化课 这天,写完数学作业之后的他脑洞大开,决定出一道比NOIP2017 D1T1<小凯的疑惑math>还要好的题 题面是这样的 $ ...

  8. [SDOI 2008]Cave 洞穴勘测

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  9. 伊布(ib)

    [问题描述]ib 被困在了一个美术馆里,她需要收集美术馆内的每种颜料才能获得逃出美术馆的钥匙美术馆由 n*m 的房间构成,每个房间里有一种颜料,解锁进入后就可以收集.有的房间不能解锁,如果解锁的话会直 ...

  10. bzoj 4894: 天赋

    Description 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有 一些天赋必须是要有前置天赋才能够学习得到的.也就是说,有一些天赋必须是要在 ...