Step by Step Explanation:
class Multiply:
def __mul__(self, other):
return "Multiplication"
Step 1: Define a Class (Multiply)
The class Multiply is defined.
It has a special method __mul__(self, other).
This method overloads the * operator.
It takes self (the current object) and other (the value being multiplied).
Instead of performing actual multiplication, it always returns the string "Multiplication".
obj = Multiply()
Step 2: Create an Object (obj)
obj is an instance of Multiply.
Now, obj has access to the __mul__ method.
print(obj * 3)
Step 3: Multiplication (obj * 3)
This calls the overloaded __mul__ method:
obj.__mul__(3)
Instead of performing normal multiplication, __mul__ returns "Multiplication".
Final Output:
"Multiplication"
0 Comments:
Post a Comment