-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperatorOverloading.py
38 lines (30 loc) · 997 Bytes
/
OperatorOverloading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#operator_overloading using dunder methods
class Number:
def __init__(self,num):
self.num= num
def __add__(self,num2):#this is example of dunder methods for add overload load
print("Let add")
return self.num+num2.num
def __sub__(self,num2):#this is example of dunder methods for minus overload
print("Let minus")
return self.num-num2.num
def __mul__(self,num2):#this is example of dunder methods for multiply overload
print("Let Multiply")
return self.num*num2.num
def __truediv__(self,num2):#this is example of dunder methods for divide overload
print("Let divide")
return self.num/num2.num
# str dunder method for print obj
def __str__(self):
return f"Number value is {self.num}"
n1=Number(12)
n2=Number(6)
sum=n1+n2
multi=n1*n2
minus=n1-n2
divide=n1/n2
print(sum)
print(minus)
print(multi)
print(divide)
print(n1)# you got object value with str here