1

I am learning Object-oriented programming through python. I am trying to write a program to write three special methods for a Fraction class that overload operators to perform rich comparisons between a and b. (a < b, a >b, a==b). A sample would be:

>>>a = Fraction(1, 2)
>>>b = Fraction(1, 3)
>>>a==b
False 
>>>a > b
True
>>>a < b
False
>>>a = Fraction(4, 8)
>>>b = Fraction(2, 4)
>>>a ==b
True
>>>a > b
False
>>>a < b
False

I really do not understand what it is asking to do. Any help on what to do to help get me started would be great. Thank you.

  • 2
    Start by looking at the Python [data model](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types). – Christian Dean Feb 24 '18 at 03:11
  • Possible duplicate of [Comprehensive guide to Operator Overloading in Python](https://stackoverflow.com/questions/2400635/comprehensive-guide-to-operator-overloading-in-python) – Omar Einea Feb 24 '18 at 03:11
  • Is this homework? And BTW it is not overloading. Python does not support overloading. – Klaus D. Feb 24 '18 at 03:14
  • @KlausD. Yes, it is. I'm not asking you to do the whole assignment I am asking what to do to get started. – David Czerwinski Feb 24 '18 at 03:24
  • This might help, https://stackoverflow.com/questions/15461574/python-overloading-operators – SteveJ Feb 24 '18 at 03:40
  • 1
    Welcome to SO. Unfortunately this isn't a discussion forum or tutorial service. Please take the time to read [ask] and the other links on that page. You should invest some time working your way through [the Tutorial](https://docs.python.org/3/tutorial/index.html), practicing the examples. It will give you an introduction to the tools Python has to offer and you may even start to get ideas for solving your problem. – wwii Feb 24 '18 at 03:40

2 Answers2

2

Here is simple code to show how this should be as an example. It is simple in python as it default have eq, ne ... you only need to change the logical part.

  class Fraction:
        a = 0
        b = 0

        def __init__(self,A,B):
            self.a=A
            self.b=B
        def __eq__(self,other):
            return (self.b/self.a) == (other.b/other.a)

        def __ne__(self,other):
            return not(self.__eq__)

        #*****************************
        # below code only demo, not for your logical
        #*****************************
        def __lt__(self,other):
            return (self.a<other.a)

        def __le__(self,other):
            return(self.a<=other.a)

        def __gt__(self,other):
            return(self.a>other.a)

        def __ge__(self,other):
            return(self.a>=other.a)



    fra_1=Fraction(4,8)
    fra_2=Fraction(2,4)
    print("(fra_1==fra_2) is:",fra_1==fra_2)
forqzy
  • 389
  • 2
  • 11
0

It's a question of implementing magic functions in classes. Below is the defined class. I have implemented for checking upto 2 decimal places.

class Fraction:
def __init__(self,*fract):
    self.fr=round(float(fract[0])/float(fract[1]),2)
def __eq__(self,f5):
    return self.fr==f5.fr
def __lt__(self,f7):
    return self.fr<f7.fr
def __gt__(self,f8):
    return self.fr>f8.fr