python如何讓類支援比較運算,python比較運算

來源:互聯網
上載者:User

python如何讓類支援比較運算,python比較運算

本文執行個體為大家分享了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)

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.