I am working on a class for polynomials and I would like to use the mul method to multiply two different polynomials when both inputs are polynomials, but also be able to use the method to multiply a polynomial with a scalar.
Is this possible? I tried searching for an answer, but only got hits related to why you can't add an integer and a string together.
Here is my incomplete function.
def __mul__(self, other):
prod = Polynomial([])
prodDict = {}
if isinstance(other, Polynomial):
return Polynomial([1])
if isinstance(other, int) or isinstance(other, float):
for i in self.poly:
prof.poly[i] = self.poly[i]*other
return Polynomial([1])
Error:
Traceback (most recent call last):
File "polynomial.py", line 118, in <module>
print(p * q)
TypeError: unsupported operand type(s) for *: 'Polynomial' and 'Polynomial'
EDIT: I made a dumb mistake and had my function nested inside the previous function by tabbing error. I apologize for wasting other's time for this trivial mistake.