国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python如何讓類支持比較運算

2020-01-04 15:23:43
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python類支持比較運算的具體代碼,供大家參考,具體內容如下

案例:

  有時我們希望自定義的類,實例間可以使用比較運算符進行比較,我們自定義比較的行為。

  需求:

    有一個矩形的類,我們希望比較兩個矩形的實例時,比較的是他們的面積

如何解決這個問題?

在類中重新定義比較運算符,所有的比較運算可以簡化為兩個基本的比較運算,小于和等于比較

單個類比較

#!/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() 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) 

會出現一個問題,重復代碼,如何解決?

通過functools下類的裝飾器total_ordering進行比較

# !/usr/bin/python3 from math import pifrom abc import abstractmethodfrom functools import total_ordering  @total_orderingclass Shape(object):  """  定義一個抽象類,重定義比較運算,再定義抽象方法,然后子類通過這個方法進行比較,  其他子類比較類都需要重構抽象方法,實現比較運算  """     # 標記比較方法,抽象方法  @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)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 女性| 阿荣旗| 两当县| 新田县| 新兴县| 响水县| 青冈县| 大城县| 乾安县| 安图县| 东乡县| 宁海县| 五台县| 莱西市| 天等县| 锡林郭勒盟| 垫江县| 永平县| 平谷区| 云浮市| 宣汉县| 咸宁市| 贞丰县| 志丹县| 翁源县| 嫩江县| 汪清县| 通榆县| 祁门县| 东乌| 大埔县| 青海省| 行唐县| 宁都县| 阿克苏市| 诸暨市| 临洮县| 于都县| 广西| 宜阳县| 东辽县|