Home > Mobile >  How to fix unsupported operand types?
How to fix unsupported operand types?

Time:07-19

from fraction.py import *

f1 = Fraction(3,4)
f2 = Fraction(2,3)
f3 = f1 * f2
print(f3)
File "python.4", line 1, in <module>
   from fraction import *
  File "/home/PYTHON/examples/fraction.py", line 50, in <module>
    f3= f1 * f2
TypeError: unsupported operand type(s) for *: 'Fraction' and 'Fraction'

There is a TypeError but I'm not sure how to correct it. I am attempting to generate a fraction. I have defined the numerator and denominator. I have the code saved on a separate file and I'm attempting to import it into the current file to apply it to this example.

CodePudding user response:

I do not know the content of the "fraction.py" file but my guess is you did not return the supported type for operand * (the common supported type would be Integer, Float...).

My suggestion is that in your custom class, you should override the magic methods __mul__(self, other).

  • Related