Skip to content

Commit 9da7d45

Browse files
committed
Added notes for Function Overloading
1 parent 510715d commit 9da7d45

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

11. Operator Overloading.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
class Polymorphism:
22
''' Polymorphism means one name, but it has different forms'''
33
class Overloading:
4-
'''Overloading refers to defining multiple functions or operators with the same name, where their parameters differ by type, number, or both. This enables the reuse of same names for different functionalities based on the context and the arguments provided.
4+
'''Overloading refers to defining multiple functions or operators with the same name, where their parameters differ by type, number, or both. This enables the reuse of same names for different functionalities based on the context and the arguments provided.'''
5+
6+
class Function_Overloading:
7+
'''Function Overloading:
8+
marking_scheme(80,90)
9+
marking_scheme(A,B)
10+
marking_scheme(80,A,90)
11+
Note: Python don't have method overloading, whatevr parameter,types, no of paramteres change for the smae fucntion name, Python will only consider the last method of the same named function. Overloading is not required for Python since, there is no datatype explicit mentioning, so need of multiple functions for types is not required. But, when it comes to number of parameters, there might be situations where we need to have different functions of the same,with different number of parameters, for different number of functionality. So, tackle this problem is, Python has 2 solutions:
12+
1) Default Arguments
13+
Here, we'll mention all the required parameters as arguments in a single method, and mark it's default value as None. Then use conditional statements, to achieve the required functionality, by seeeing the user's input.
14+
2) Variable Arguments
15+
It is not possible to have all the number of parameters already known or fixed, so use *args as paramter, which means variable arguments, which is a Tuple, with that u can have as much values as parameters, using a single function itself.'''
16+
17+
def __init__(self,*a):
18+
print("Constructor with any number of arguments from [0,N]")
519

6-
1) Function Overloading:
7-
marking_scheme(80,90)
8-
marking_scheme(A,B)
9-
marking_scheme(80,A,90)
10-
Note: Python don't have method overloading, whatevr parameter,types, no of paramteres change for the smae fucntion name, Python will only consider the last method of the same named function. Since, there is no datatype explicit mentioning, overloading was not needed for Python.
11-
12-
2)Operator Overloading:
20+
21+
def default_Arguments_sum(self, a= None, b = None, c = None):
22+
print("here, we shold not give 0 as argument default value instead of None, since if 0 is the actaul number value, 0nto only means a number,it also means OFF state, 0 could mention , there is no number also.SO, use None only")
23+
if a!=None and b!=None and c!=None:
24+
print("Sum of the 3 numbers = ",a+b+c)
25+
elif a!= None and b!=None:
26+
print("Sum of 2 numbers is",a+b)
27+
else:
28+
print("Provide atleast 2 numbers to compute")
29+
30+
def variable_arguments_sum(self,*a):
31+
total = 0
32+
for no in a:
33+
total+=no
34+
35+
return total
36+
37+
38+
39+
class Operator_Overloading:
40+
''' Operator Overloading:
1341
It enables to use the same operator for whatever objects we want.
1442
e.,g 'daily_wage+'kumar' = 'daily_wagekumar'
1543
'daily_wage'*3 = 'daily_wagedaily_wagedaily_wage'
@@ -77,9 +105,10 @@ class Overloading:
77105
__ilshift__(): <<=
78106
__irshift__(): >>=
79107
80-
'''
81-
class Operator_Overloading:
82-
'''Whenver we perform operator overloading, the corresponding magic method is called, e.g, ibject A + object B, it's a bianry operator,s o 2 paramters woudl be there ,then we use __add__(self, other) Here the self instance variable acts, as the 1st operand, whcih is left to teh '+' operator i.e object A, and the other refers to teh 2nd operand, which is in right side of the '+; operator i.e object B. '''
108+
109+
110+
111+
Whenver we perform operator overloading, the corresponding magic method is called, e.g, ibject A + object B, it's a bianry operator,s o 2 paramters woudl be there ,then we use __add__(self, other) Here the self instance variable acts, as the 1st operand, whcih is left to teh '+' operator i.e object A, and the other refers to teh 2nd operand, which is in right side of the '+; operator i.e object B. '''
83112

84113
def __init__(self,value):
85114
self.value = value
@@ -120,9 +149,17 @@ def __str__(self):
120149

121150

122151
p = Polymorphism()
152+
123153
op = p.Overloading()
124154
print(p.__doc__)
125155
print(op.__doc__)
156+
print(op.Function_Overloading.__doc__)
157+
opf = op.Function_Overloading(5,10,5,5,6013,156,0)
158+
opf.default_Arguments_sum(1,3)
159+
opf.default_Arguments_sum(1,3,2)
160+
print("Var arg ouput",opf.variable_arguments_sum(1,2,3,5,100,6,8,6,3,5))
161+
print("Var arg ouput",opf.variable_arguments_sum(1,2,3))
162+
print("Var arg ouput",opf.variable_arguments_sum())
126163
print(op.Operator_Overloading.__doc__)
127164
print("You can find all teh avilbe magic emthod,of ur data type uisng, ",dir(int))
128165
a = op.Operator_Overloading(2)

0 commit comments

Comments
 (0)