How does python make classes support comparison and python comparison operations?
The examples in this article share with you the python code that supports comparison operations for your reference. The details are as follows:
Case:
Sometimes we want to customize the class, instances can use comparison operators for comparison, we customize the comparison behavior.
Requirements:
There is a rectangle class. When we want to compare two rectangle instances, we compare their area.
How can this problem be solved?
Define a comparison operator in a class. All comparison operations can be simplified to two basic comparison operations.
Single class comparison
#! /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) # redefinition is smaller than comparison def _ lt _ (self, other): return self. get_area () <other. get_area () # redefinition equals to comparison of 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)
Comparison of Two Classes
#! /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) # redefinition is smaller than comparison def _ lt _ (self, other): return self. get_area () <other. get_area () # redefinition equals to comparison of 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) # redefinition is smaller than comparison def _ lt _ (self, other): return self. get_area () <other. get_area () # redefinition equals to comparison of 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 # redefinition is less than comparison def _ lt _ (self, other): return self. get_area () <other. get_area () # redefinition equals to comparison of 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)
There will be a problem, repeated code, how to solve?
Use total_ordering in the functools class to compare
#! /Usr/bin/python3 from math import pifrom abc import abstractmethodfrom functools import total_ordering @ total_orderingclass Shape (object): "" defines an abstract class, redefines comparison operations, and then Defines abstract methods, then the subclass uses this method for comparison. Other subclass comparison classes must reconstruct the abstract method to implement the comparison operation "# mark the comparison method. The abstract method @ abstractmethod def get_area (self ): pass # redefinition is less than comparison def _ lt _ (self, other): return self. get_area () <other. get_area () # redefinition equals to comparison of 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)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.