Method overloading
Python hosting: Host, run, and code Python in the cloud!
In Python you can define a method in such a way that there are multiple ways to call it.
Given a single method or function, we can specify the number of parameters ourself.
Depending on the function definition, it can be called with zero, one, two or more parameters.
This is known as method overloading. Not all programming languages support method overloading, but Python does.
Related course
Python Programming Bootcamp: Go from zero to hero
Method overloading example
We create a class with one method sayHello(). The first parameter of this method is set to None, this gives us the option to call it with or without a parameter.
An object is created based on the class, and we call its method using zero and one parameter.
#!/usr/bin/env python |
Output:
Hello |
To clarify method overloading, we can now call the method sayHello() in two ways:
obj.sayHello() |
We created a method that can be called with fewer arguments than it is defined to allow.
We are not limited to two variables, your method could have more variables which are optional.
If you are new to Python programming, I highly recommend this book.
Leave a Reply:
So to clarify, if no argument is given for the second parameter, the second parameter will be set to None?
Yes, if no parameter is given it will be set to None. There is no limit to the number of parameters you pass.
You could have a method that accepts multiple parameters:
What abou situation: we have no first argument, and have the second?
Hi Andrew, you could pass the first parameter as None. An alternative would be to pass an instance of a class (an object) to a method, which is what I recommend if you want to pass a lot of variables.
Can you have other variables in overloading besides none? Such as def sayHello(self, name='Default User'): ?
This seems to work. Usually constant variables are defined in the local scope of the function, you could use it as default value for a parameter.
Thank you. It looks easier)
Hello, how can i do that?(last command)
Try None for the first argument.
it didn't work for me by writing none for the first arguement !!
Change the function to:
Dear Frank,
i want some help in packages. i have not found any article related to packages on this website. sorry i am posting my question in this method overloading section. I have made three packages named Data, Student and postgraduate. postgraduate is subpackage of student package. there are files : biodata.py and _init_.py in student and subjects.py and_init_.py in postgraduate respectively.
code in biodata.py:
code in subject.py
in package Data there are two files _init_.py and test.py
i want to access the methods of student_info and info into Data package. i mean by running Data package i could see output of these methods here. for this purpose i have written this code in test.py
i am getting error on object creation. (obj=Student_Info() and obj2=Info() . please tell me what i am doing wrong in this program.
thank you
Hi Sadia,you are missing some imports:
Presently an object of student_info is not given a name or age. We can set the object variables by using a constructor.
I've changed biodata.py (Student_Info) class:
Changed test.py to
try last function as following
obj.sayhello(age=23)
__author__ = 'jatin'
try this i m getting output
Just short notice about boolean condition. It could be shortened from the form:
to