|
1 | 1 | class Polymorphism:
|
2 | 2 | ''' Polymorphism means one name, but it has different forms'''
|
3 | 3 | 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]") |
5 | 19 |
|
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: |
13 | 41 | It enables to use the same operator for whatever objects we want.
|
14 | 42 | e.,g 'daily_wage+'kumar' = 'daily_wagekumar'
|
15 | 43 | 'daily_wage'*3 = 'daily_wagedaily_wagedaily_wage'
|
@@ -77,9 +105,10 @@ class Overloading:
|
77 | 105 | __ilshift__(): <<=
|
78 | 106 | __irshift__(): >>=
|
79 | 107 |
|
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. ''' |
83 | 112 |
|
84 | 113 | def __init__(self,value):
|
85 | 114 | self.value = value
|
@@ -120,9 +149,17 @@ def __str__(self):
|
120 | 149 |
|
121 | 150 |
|
122 | 151 | p = Polymorphism()
|
| 152 | + |
123 | 153 | op = p.Overloading()
|
124 | 154 | print(p.__doc__)
|
125 | 155 | 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()) |
126 | 163 | print(op.Operator_Overloading.__doc__)
|
127 | 164 | print("You can find all teh avilbe magic emthod,of ur data type uisng, ",dir(int))
|
128 | 165 | a = op.Operator_Overloading(2)
|
|
0 commit comments