python如何讓類支持比較運(yùn)算
本文實(shí)例為大家分享了python類支持比較運(yùn)算的具體代碼,供大家參考,具體內(nèi)容如下
案例:
有時(shí)我們希望自定義的類,實(shí)例間可以使用比較運(yùn)算符進(jìn)行比較,我們自定義比較的行為。
需求:
有一個(gè)矩形的類,我們希望比較兩個(gè)矩形的實(shí)例時(shí),比較的是他們的面積
如何解決這個(gè)問題?
在類中重新定義比較運(yùn)算符,所有的比較運(yùn)算可以簡化為兩個(gè)基本的比較運(yùn)算,小于和等于比較
單個(gè)類比較
#!/usr/bin/python3
from math import pi
class Circle(object):
def __init__(self, radius):
self.radius = radius
def get_area(self):
return round(pow(self.radius, 2) * pi, 2)
# 重定義小于比較
def __lt__(self, other):
return self.get_area() < other.get_area()
# 重定義等于比較
def __eq__(self, other):
return self.get_area() == other.get_area()
if __name__ == '__main__':
c1 = Circle(3.0)
c2 = Circle(5.0)
print(c1 < c2) # c1.__le__(c2)
print(c1 == c2) # c1.__eq__(c2)
兩個(gè)類比較
#!/usr/bin/python3
from math import pi
class Circle(object):
def __init__(self, radius):
self.radius = radius
def get_area(self):
return round(pow(self.radius, 2) * pi, 2)
# 重定義小于比較
def __lt__(self, other):
return self.get_area() < other.get_area()
# 重定義等于比較
def __eq__(self, other):
return self.get_area() == other.get_area()
if __name__ == '__main__':
c1 = Circle(3.0)
c2 = Circle(5.0)
print(c1 < c2) # c1.__le__(c2)
print(c1 == c2) # c1.__eq__(c2)
# !/usr/bin/python3
from math import pi
class Circle(object):
def __init__(self, radius):
self.radius = radius
def get_area(self):
return round(pow(self.radius, 2) * pi, 2)
# 重定義小于比較
def __lt__(self, other):
return self.get_area() < other.get_area()
# 重定義等于比較
def __eq__(self, other):
return self.get_area() == other.get_area()
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
def get_area(self):
return self.width * self.height
# 重定義小于比較
def __lt__(self, other):
return self.get_area() < other.get_area()
# 重定義等于比較
def __eq__(self, other):
return self.get_area() == other.get_area()
if __name__ == '__main__':
c1 = Circle(5.0)
R1 = Rectangle(4.0, 5.0)
print(c1 > R1) # c1.__le__(c2)
print(c1 == R1) # c1.__eq__(c2)
會出現(xiàn)一個(gè)問題,重復(fù)代碼,如何解決?
通過functools下類的裝飾器total_ordering進(jìn)行比較
# !/usr/bin/python3
from math import pi
from abc import abstractmethod
from functools import total_ordering
@total_ordering
class Shape(object):
"""
定義一個(gè)抽象類,重定義比較運(yùn)算,再定義抽象方法,然后子類通過這個(gè)方法進(jìn)行比較,
其他子類比較類都需要重構(gòu)抽象方法,實(shí)現(xiàn)比較運(yùn)算
"""
# 標(biāo)記比較方法,抽象方法
@abstractmethod
def get_area(self):
pass
# 重定義小于比較
def __lt__(self, other):
return self.get_area() < other.get_area()
# 重定義等于比較
def __eq__(self, other):
return self.get_area() == other.get_area()
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def get_area(self):
return round(pow(self.radius, 2) * pi, 2)
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def get_area(self):
return self.width * self.height
if __name__ == '__main__':
c1 = Circle(5.0)
R1 = Rectangle(4.0, 5.0)
print(c1 > R1) # c1.__le__(c2)
print(c1 == R1) # c1.__eq__(c2)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 五子棋如何獲得鼠標(biāo)點(diǎn)擊坐標(biāo)
這篇文章主要介紹了python 五子棋如何獲得鼠標(biāo)點(diǎn)擊坐標(biāo),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python實(shí)現(xiàn)得到一個(gè)給定類的虛函數(shù)
這篇文章主要介紹了python實(shí)現(xiàn)得到一個(gè)給定類的虛函數(shù)的方法,以wx的PyPanel類為例講述了打印以base_開頭的方法的實(shí)例,需要的朋友可以參考下2014-09-09
Python實(shí)現(xiàn)解析Html的方法與對比
在最近需要的需求中,需要?python?獲取網(wǎng)頁內(nèi)容,并從html中獲取到想要的內(nèi)容,本文主要介紹了兩種常用方法并進(jìn)行了對比,感興趣的可以了解下2024-03-03
Python使用itcaht庫實(shí)現(xiàn)微信自動收發(fā)消息功能
這篇文章主要介紹了Python使用itcaht庫實(shí)現(xiàn)微信自動收發(fā)消息功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

