Method Overloading Simulation
Simulate method overloading using default arguments and *args.
BeginnerTopic: Object-Oriented Programs
Python Method Overloading Simulation Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to simulate method overloading
class Adder:
def add(self, *args):
return sum(args)
a = Adder()
print(a.add(1, 2))
print(a.add(1, 2, 3))Output
3 6
Understanding Method Overloading Simulation
Python does not support true method overloading, but *args lets one method handle varying numbers of arguments.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.