0
0
Pythonprogramming~10 mins

Arithmetic operator overloading in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic operator overloading
Create object with value
Use operator like +, -, *
Call special method (__add__, __sub__, __mul__)
Perform custom operation
Return new object or value
Use result in expression
When you use operators like + or -, Python calls special methods in your class to decide what to do.
Execution Sample
Python
class Number:
    def __init__(self, value):
        self.value = value
    def __add__(self, other):
        return Number(self.value + other.value)

n1 = Number(3)
n2 = Number(4)
n3 = n1 + n2
print(n3.value)
This code adds two Number objects by overloading the + operator and prints the result.
Execution Table
StepActionEvaluationResult
1Create n1 with value 3n1.value = 3n1 = Number(3)
2Create n2 with value 4n2.value = 4n2 = Number(4)
3Evaluate n1 + n2Calls n1.__add__(n2)Returns Number with value 7
4Assign result to n3n3.value = 7n3 = Number(7)
5Print n3.valueOutput is 77
💡 Finished printing result, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
n1.valueundefined33333
n2.valueundefinedundefined4444
n3.valueundefinedundefinedundefinedundefined77
Key Moments - 3 Insights
Why does n1 + n2 call the __add__ method?
Because Python uses the __add__ method to define what + means for objects, as shown in step 3 of the execution_table.
What type is the result of n1 + n2?
The result is a new Number object with value 7, not just a number, as seen in step 3 and 4.
Why do we access n3.value to print the number?
Because n3 is a Number object, and the actual number is stored in its value attribute, shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of n3.value after step 4?
A3
B4
C7
Dundefined
💡 Hint
Check the 'After Step 4' column for n3.value in variable_tracker.
At which step does Python call the __add__ method?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the 'Action' column in execution_table where n1 + n2 is evaluated.
If __add__ returned self.value + other.value instead of a Number, what would change?
An3 would be an int, not a Number object
Bn1 and n2 would change values
Cn3 would be a Number object
DThe program would crash
💡 Hint
Think about the return type in step 3 of execution_table.
Concept Snapshot
Arithmetic operator overloading lets you define how operators like + work on your objects.
Use special methods like __add__(self, other) to customize behavior.
Return a new object or value from these methods.
This lets you write expressions like obj1 + obj2 naturally.
Python calls these methods automatically when operators are used.
Full Transcript
This lesson shows how Python lets you change what operators like + do when used with your own objects. When you write n1 + n2, Python calls the __add__ method inside your class. This method can do anything you want, like adding values inside the objects and returning a new object. We traced a simple example where two Number objects are added. Step by step, we saw how objects are created, how __add__ is called, and how the result is stored and printed. Key points include understanding that operators call special methods, the result can be a new object, and you access the stored value to see the number. The quizzes check your understanding of these steps and what changes if the method returns a different type.