<p style="">Python has magic methods to define overloaded behaviour of operators. The comparison operators (&lt;, &lt;=, &gt;, &gt;=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods.&nbsp;</p><p style="">Following program overloads == and &gt;= operators to compare objects of distance class.</p><pre class="prettyprint notranslate">class distance: &nbsp; &nbsp; &nbsp; def __init__(self, x=5,y=5): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ft=x &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.inch=y &nbsp; &nbsp; &nbsp; def __eq__(self, other): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if self.ft==other.ft and self.inch==other.inch: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;both objects are equal&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;both objects are not equal&quot; &nbsp; &nbsp; &nbsp; &nbsp;def __ge__(self, other): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in1=self.ft*12+self.inch &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in2=other.ft*12+other.inch &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if in1&gt;=in2: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;first object greater than or equal to other&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;first object smaller than other&quot; d1=distance(5,5) d2=distance() print (d1==d2) d3=distance() d4=distance(6,10) print (d1==d2) d5=distance(3,11) d6=distance() print(d5&gt;=d6)</pre><p style="">Result of above program shows overloaded use of == and &gt;= comparison operators</p><pre class="result notranslate">both objects are equal both objects are equal first object smaller than other</pre> Overload Python Comparison Operators

Overload Python Comparison Operators



Python has magic methods to define overloaded behaviour of operators. The comparison operators (<, <=, >, >=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods. 

Following program overloads == and >= operators to compare objects of distance class.

class distance:
      def __init__(self, x=5,y=5):
            self.ft=x
            self.inch=y

      def __eq__(self, other):
             if self.ft==other.ft and self.inch==other.inch:
                  return "both objects are equal"
             else:
                  return "both objects are not equal"

       def __ge__(self, other):
                 in1=self.ft*12+self.inch
                 in2=other.ft*12+other.inch
                 if in1>=in2:
                    return "first object greater than or equal to other"
                  else:
                    return "first object smaller than other"

d1=distance(5,5)
d2=distance()
print (d1==d2)
d3=distance()
d4=distance(6,10)
print (d1==d2)
d5=distance(3,11)
d6=distance()
print(d5>=d6)

Result of above program shows overloaded use of == and >= comparison operators

both objects are equal
both objects are equal
first object smaller than other
Updated on: 2020-03-02T08:17:26+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements