Creating a custom float class by inheritance in python

Viewed 554

I am trying to create a custom float class 'Float' in Python by inheriting the float class and overloading the operators associated with it. I am overloading the arithmetic operators associated with the class so as to obtain the precision of the decimal.Decimal class for arithmetic calculations while maintaining the performance of the float class which is far better than that of the decimal.Decimal class.

This way, I do not need to use the round() function or string formatting whenever I need to display a human readable output.

This is my code with only the addition operator overloaded:

class Float(float): #custom float class
    def Float(self,value): #Constructor like method
        self.value = value

    def count_decimal_places(float_num): #Counts decimal places for rounding
        if type(float_num) == int:
            return 0
        return len(str(float_num).split('.')[1])

    def __add__(self, other): #rounds results to the higher number of decimal places
        return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))

fo = Float()
fo.Float(3.6)
fo2 = Float()
fo2.Float(5.6)
print(fo + fo2)

When I used a constructor directly in the class to assign a value to the 'value' attribute, that did not work.

    class Float(float):
            def init(self,value):
                    self.value = value
            def count_decimal_places(float_num):
                    if type(float_num) == int:
                            return 0
                    return len(str(float_num).split('.')[1])
            def __add__(self, other):
                    return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value))

    fo = Float(3.6)
    fo2 = Float(5.6)
    print(fo + fo2)        

It generated a syntax error. This is why I used a constructor like method instead in the first code.

Also the init method exists for class float in Python and shows up in print(dir(float)) but I am unable to ascertain what it does so I have not used the predefined constructor of class float.

1 Answers

You had mismatching parantheses at the return line of def __add__(self, other). After fixing it I've put the constructor back. You also forgot to put self as the first parameter in count_decimal_places so I fixed that too. I didn't check the logic of your functions though, but it seems to be working so far:

class Float(float): #custom float class
    def __init__(self,value): #Constructor like method
        self.value = value

    def count_decimal_places(self, float_num): #Counts decimal places for rounding
        if type(float_num) == int:
            return 0
        return len(str(float_num).split('.')[1])

    def __add__(self, other): #rounds results to the higher number of decimal places
        return round(self.value +other.value, max(self.count_decimal_places(self.value), self.count_decimal_places(other.value)))

fo = Float(3)
fo2 = Float(5.6)
print(fo+fo2)

Output: 8.6

Related