Method overloading example in python
class Localbus: def sayLocal(self, busName=None): if busName is not None: print('Hello ' + busName) else: print('Hello ') obj = Localbus() obj.sayLocal() obj.sayLocal('Alif')
Output
=============
Hello
Hello Alif
class Human: def sayHello(self, name=None, age=None): if name is not None and age is None: print('Hello ' + name) elif age is not None and age is not None: print('Hello ',name, ' your are ' ,age,' years old !') else: print('Hello ') # Create instance obj = Human() # Call the method obj.sayHello() # Call the method with a parameter obj.sayHello('Ad', '23') obj.sayHello(age=23)
Output
==============
Hello
Hello Ad your are 23 years old !
Hello None your are 23 years old !