How does python make classes support comparison and python comparison operations?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.