简体   繁体   中英

How to fix unsupported operand types?

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM