-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.py
More file actions
163 lines (107 loc) · 3.16 KB
/
Copy pathoops.py
File metadata and controls
163 lines (107 loc) · 3.16 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# class Animal:
# name = "Lion" #"Public attribute"
# _age = 12 #protected attribute
# __height = 120 ## private attribute
# def speak (self):
# print("the lion roars")
# def _walk(self):
# print("the lion is walking")
# def __sleep(self): #private method
# print("the lion is sleeping")
# obj1 = Animal()
# #print(obj1.__height)
# obj1._walk()
# ##private attribute and method cannot be accessed by your objects and inherited classess
# ######### POLYMORPHISM ########
# class Animal:
# def speak(self):
# print("animal are shouting")
# class Human:
# def speak(self):
# print("humans are intelligent so they are speaking")
# obj1 = Animal()
# obj2 = Human()
# obj1.speak()
# obj2.speak()
# ## both the speak method appear to be
# #same but both have different task and this is known as polymorphism
# ### Method overriding
# class Reebok:
# def __init__(self,material,size):
# self.material = material
# self.size = size
# def details(self):
# print("your bag detail is :")
# print(self.material)
# print(self.size)
# class Campus(Reebok):
# def __init__(self,material,size,color):
# super().__init__(material,size)
# self.color = color
# def detail(self):
# print(self.color)
# print(super().details())
# obj1 = Campus("leather",10,"black")
# obj1.detail()
## when class object has the power to call mwthods and attributes
#of a parent class but he cannot call the details
## method of his parent class cause that details methods
#is overrridden and this concept is known as method overriding
##overloading
# class Animal:
# def hello(self,a):
# print("how are you")
# def hello(self,a,b):
# print("how are you man")
# obj = Animal()
# obj.hello()
## method overloading is a concept where you define similar name method inside a single class with different parameters
### Abstraction
from abc import ABC , abstractmethod
class shapes(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Square(shapes):
def __init__(self,side):
self.side = side
def perimeter(self):
print(4* self.side)
def area(self):
print(self.side * self.side)
class circle(shapes):
def __init__(self,radius):
self.radius = radius
def area():
pass
def perimeter():
pass
obj = Square(10)
obj.area()
obj.perimeter()
######### DUNDER METHOD /magic method #######################
class Robots:
a = 12
def __init__(self,name):
self.name = name
def __str__(self):
return f"hello my name is {self.name}"
obj = Robots("alpha1")
obj1 = Robots("beta1")
print(obj)
print(obj1)
###__add__ method
class Numbers:
def __init__(self,value):
self.value = value
def __add__(self,other):
return self.value + other.value
def __eq__(self,value):
return self.value == value.value
a = Numbers(20)
b = Numbers(30)
print(a == b)
print(a + b)