Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Overloading greater than operator in python causes infinite recursion

I understand how to overload an operator for classes in general and magic methods. I am trying to overload "greater than" or > but every solution I am developing causes an infinite recursion. I've worked on this for hours and I am not getting anywhere and would appreciate some help from a python maven.

The fraction constructor works. It's using the magic method gt_ to overload ">' that isn't.

Here is the code for my class:

import fractions


class Fraction:
    """

    """

    def __init__(self, num, den):
        if den == 0:
            raise ValueError("Zero denominator is illegal.")

        self.num = num
        self.den = den

    def get_num(self):
        return self.num

    def get_den(self):
        return self.den

    def string(self):
        return '{}/{}'.format(self.num, self.den)

    def __gt__(self, fract_ov):
        return Fraction(self.num, self.den) > Fraction(fract_ov.get_num, fract_ov.get_den)

    @property
    def simplify(self):
        """

        """
        fact = gcd(self.num, self.den)
        return Fraction(self.num // fact, self.den // fact)


def gcd(a, b):
    """
    """

    def common(a, b):
        return [i for i in a if i in b]

    def div(n):
        return [i for i in range(1, n + 1) if n % i == 0]

    return max(common(div(a), div(b)))


def main():
    print(Fraction.string(Fraction(3, 5)))
    fract1 = Fraction(5, 3)
    fract2 = Fraction(4, 5)
    if fract1 > fract2:
        print("fract1 wins")
    else:
        print("fract2 wins")


main()

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

Using the > operator is exactly equivalent to calling __gt__ on an instance of an object. Since you're overriding __gt__ on Fraction, and then using the > operator on 2 Fraction instances, it will do nothing but overflow the call stack. Instead, you'll need to call > on the parts that make up a Fraction. For example, a way you might do this would be to divide self's numerator by its denominator, and then divide fract_ov's numerator by its denominator, and then compare those results with >. That way, you'll be comparing floats, and your implementation will be deferring to the __gt__ implementation on float instead of calling itself again

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Thanks. I also implemented the magic method for div and divided the individual fractions. It worked.