1. < 对应 __lt__
2. <= 对应 __le__
3. > 对应 __gt__
4. >= 对应 __ge__
5. == 对应 __eq__
6. != 对应 __ne__
- 相同类型的比较
class Rectangle(object):
def __init__(self,w,h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
# <
def __lt__(self,obj):
print('__lt__') # 打印,说明进来
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() < obj.area()
# <=
def __le__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() <= obj.area()
# >
def __gt__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() > obj.area()
# >=
def __ge__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() >= obj.area()
# ==
def __eq__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() == obj.area()
# !=
def __ne__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() != obj.area()
r1 = Rectangle(3,3)
r2 = Rectangle(4,4)
r3 = Rectangle(4,4)
print(r1 < r2) # True
print(r1 <= r2) # True
print(r1 > r2) # False
print(r1 >= r2) # False
print(r2 == r3) # True
print(r2 != r3) # False
s = 16
print(r2 == s) # TypeError: obj is not Rectangle
- 相同类型的比较操作(functools.total_ordering,类装饰器)
from functools import total_ordering
@total_ordering
class Rectangle(object):
def __init__(self,w,h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
# <
def __lt__(self,obj):
print('__lt__') # 总共被调用4次 < ,<= ,> ,>=
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() < obj.area()
# ==
def __eq__(self,obj):
# 对类型进行检查
if not isinstance(obj,Rectangle):
raise TypeError('obj is not Rectangle')
return self.area() == obj.area()
""''
用functools.total_ordering模块的方法,只需要实现<,==即可
<= 对应 < or ==
> 对应 not(< or ==)
>= 对应 not <
!= 对应 not ==
"""
r1 = Rectangle(3,3)
r2 = Rectangle(4,4)
r3 = Rectangle(4,4)
print(r1 < r2) # True
print(r1 <= r2) # True
print(r1 > r2) # False
print(r1 >= r2) # False
print(r2 == r3) # True
print(r2 != r3) # False
s = 16
print(r2 == s) # TypeError: obj is not Rectangle
- 不同类型的比较操作(写抽象方法,继承)
import math
from functools import total_ordering
from abc import abstractmethod
@total_ordering
class Shape(object):
# 写抽象方法
@abstractmethod
def area(self):
pass # 什么也不做
# <
def __lt__(self,obj):
print('__lt__') # 总共被调用4次 < ,<= ,> ,>=
# 对类型进行检查
if not isinstance(obj,Shape):
raise TypeError('obj is not Shape')
return self.area() < obj.area()
# ==
def __eq__(self,obj):
# 对类型进行检查
if not isinstance(obj,Shape):
raise TypeError('obj is not Shape')
return self.area() == obj.area()
# 继承Shape
class Rectangle(Shape):
def __init__(self,w,h):
self.w = w
self.h = h
# 实现area
def area(self):
return self.w * self.h
# 继承Shape
class Circle(Shape):
def __init__(self,radius):
self.radius = radius
# 实现area
def area(self):
return self.radius * 2 * math.pi
r1 = Rectangle(4,4)
r2 = Rectangle(4,4)
r3 = Circle(4)
print(r2 < r3) # True
print(r2 <= r3) # True
print(r2 > r3) # False
print(r2 >= r3) # False
print(r1 == r2) # True
print(r1 != r2) # False
s = 16
print(r2 == s) # TypeError: obj is not Shape