Method Overloading in Python — Python Technical Interview Questions & Answers
Method Overloading in Python
Method Overloading in Python
- Java-style overloading — same method name, many signatures at compile time — is not how Python works. A second def with the same name replaces the first. To accept optional arguments, use defaults, *args, **kwargs, or functools.singledispatch. Overriding in a child class is normal and common.
- Be honest: Python “overloads” via defaults and *args, not via many typed defs. Contrast overloading (same class, many signatures) vs overriding (child replaces parent). Freshers mix those two words.
- Method Overloading in Python — output: 5 then 5. One function, default b=0 — Python’s way, not two add methods.
Technical Interview Tip: Overloading vs overriding in Python.
Code Examples for Method Overloading in Python
Example
# Overloading idea
def add(a, b=0):
return a + b
print(add(2, 3))
print(add(5))
Method Overloading in Python — output: 5 then 5. One function, default b=0 — Python’s way, not two add methods.
How to Explain Method Overloading in Python to an Interviewer
Explain Method Overloading as if you are teaching a junior — definition, then one tiny script.
No Java-style overload Later def wins
What does the example print, and why?
Method Overloading in Python — output: 5 then 5
What mistake do freshers make with Method Overloading?
Use defaults / *args