Today let us learn how to overload functions and constructors of a
class.
Overloading user-defined functions
The user defined functions can be
overloaded, by giving the functions
same name but passing different number of arguments. But Python does not provide such facility. It takes the latest version
(the last version) and executes it. Thus if lesser parameters are passed,
error arises. For example:
Here the
addition ( ) function has
been overloaded. If this was a C program and if you pass 2 numbers then
additon ( a,b ) will be
called. If you pass 3 numbers, then
addition ( a,b,c ) will be
called. But in Python it considers the last defined version only and here the
latest one ( recent one) is
addition (a,b,c). Now
if you pass only 2 arguments it results in error.
The problem mentioned above can be overcome by mentioning the
number of arguments before-handed. Look at the code below.
Here, n denotes
the number of arguments passed and
*args takes the values to be
added.
*args in python
This is used when we need to pass variable number of inputs to a function.
Here placing an asterisk '*' symbol makes the following variable as an
iterable. Now
args is an
iterable. In this method we have to specify the number of arguments before
handed. To avoid that we can slightly modify the code as given below.
To make the function accept arbitrary number of values, instead of
if , one can write it in a
loop as given below.
Now how about overloading a method based on the datatype passed to it !!!?
Consider the same
addition ( ). If numbers are passed it has to sum them up and return the result. If Stings
are passed it should concatenate them and return the result. Look at the code
below.
Overloading the constructors
The class has constructor which is automatically invoked when an instance of
the class is created. To know about constructors read
this post. The constructors either initializes the supplied values to the variables or
initializes them to '0'. This can be done based on the number of arguments
passed. There is no explicit constructor overloading as in C or C++. You can
make it work differently based on the number of arguments passed.
Consider the
addition concept for
two equations where coefficient of appropriate terms must be added. If no
arguments are passed its variables must be 0. Otherwise it should be
initialized based on the length of the arguments passed. Look at the example
below.
class arithmetic:
def __init__(self,*args):
if len(args)==0:
self.x=0
self.y=0
elif len(args)==1:
self.x=args[0]
self.y=0
elif len(args)==2:
self.x=args[0]
self.y=args[1]
else:
print("Object creation
error!!!") #for restricting input size to 2
def addition(self,z):
a=self.x+z.x
b=self.y+z.y
return [a,b]
eq1=arithmetic() #no argument
eq2=arithmetic(4) # one argument
eq3=arithmetic(8,23) # two argument
print(eq1.addition(eq3))
print(eq2.addition(eq3))
eq4=arithmetic(5,3,4) #more number of arguments
[8, 23]
[12, 23]
Object creation error!!!
Next Topic👉
Comments
Post a Comment