-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13_MagicMethods_1.py
61 lines (51 loc) · 1.63 KB
/
13_MagicMethods_1.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#C0d3 n0=13
#made By GuND0Wn151
"""
there are some methods in python class
Magic methods in Python are the special methods which add "magic"
to your class. Magic methods are not
meant to be invoked directly by you, but the invocation happens
internally from the class on a certain action.
"""
"""
The below is example of overloading the arthematic methods like addition subtraction etc
whihc also returns a object of that class
"""
class Point:
x=0
y=0
def __init__(self,a,b):
self.x=a
self.y=b
def __add__(self,a):
return Point(self.x+a.x ,self.y+a.y)
def __sub__(self,a):
return Point(self.x-a.x ,self.y-a.y)
def __mul__(self,a):
return Point(self.x*a.x ,self.y*a.y)
def __floordiv__(self,a):
return Point(self.x/a.x ,self.y/a.y)
def displayDetails(self):
print("X:",self.x,', Y:',self.y)
a=Point(2,3)
b=Point(4,6)
# here + will invoke the overloaded __add__ method in the class and return a object of type Point
f=a+b
# here - will invoke the overloaded __sub__ method in the class and return a object of type Point
g=a-b
# here * will invoke the overloaded __mul__ method in the class and return a object of type Point
h=a*b
# here // will invoke the overloaded __floordiv__ method in the class and return a object of type Point
i=a//b
"""
the return type of the overloaded arthematic options are Point
so we acces them with the method displayDetails
"""
print(type(f))
f.displayDetails()
print(type(g))
g.displayDetails()
print(type(h))
h.displayDetails()
print(type(i))
i.displayDetails()