Question: 1. Consider the following code: class MyOperand: def __init__(self, op): self.op1 = op def __add__(self, otherOperand): return 'Result:', self.op1 + otherOperand.op1 x = MyOperand(4) y
1. Consider the following code:
class MyOperand:
def __init__(self, op):
self.op1 = op
def __add__(self, otherOperand):
return 'Result:', self.op1 + otherOperand.op1
x = MyOperand(4)
y = MyOperand(5)
print(x+y)
x = MyOperand("H")
y = MyOperand("i")
print(x+y)
In the above code, when two instances of the class MyOperand i.e. x and y are added with
the + operator we see that the built-in function __add__ is called. Here, the + operator was
overloaded to add two numbers or concatenate two strings with a note that reads Result.
1- Include the __sub__ function in the above code and test it with numbers, strings, and
lists. Explain the results.
i need the answer in python code please?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
