-
Notifications
You must be signed in to change notification settings - Fork 2
/
Python_OOP.py
373 lines (201 loc) · 5.57 KB
/
Python_OOP.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
## Python Object Oriented
## Content:
## 1- EXCEPTIONS
## 2- Python Classes
## 3- Getters and setters
## 4- Inheritance
## 5- Method overriding && overloading
## 6- Class variable
## EXCEPTIONS
## when procedure execution hits an unexpected condition
## Types of exceptions:
## SyntaxError : Python can’t parse program
## NameError : local or global name not found
## IndexError
## AttributeError: attribute reference fails
## TypeError : operand doesn’t have correct type
## ValueError: operand type okay, but value is illegal
## ZeroDivisionError
## Name error
# print(pp)
## IndexError
# l = [1, 2, 3]
# print(l[4])
## AttributeError
# a = 5
# a.append(3)
## TypeError
# s = "This is string"
# a = s / 5
# def func(a,b):
# return a + b
# func(5,"hossam")
## zero division error
# a = 5
# b = 0
# print(a/b)
# # Dealing with exception
# try:
# # type your code here
# a = 5
# b = 0
# print(a/b)
# except:
# print("There is abug in code")
# # # Separate except clauses
# try:
# # type your code here
# a = 5
# b = 1
# print(a/0)
# except ZeroDivisionError:
# print("There is a zero division error here")
# except ValueError:
# print("There is a Value error here")
# except TypeError:
# print("There is a type error here")
# except:
# print("There is a bug here")
# # else:
# # print("This code excute if there is no error")
# finally:
# print("This code excute if there is an error")
# OOP [EVERYTHING IN PYTHON IS AN OBJECT]
# # Creating a class
# class Coordinate():
# def __init__(self, x, y):
# self.x = x
# self.y = y
# c = Coordinate(3, 5)
# origin = Coordinate(0, 0)
# print(c.x)
# print(origin.y)
# class Coordinate():
# def __init__(self, x, y):
# self.x = x
# self.y = y
# def distance(self, other):
# x_diff = (self.x - other.x) ** 2
# y_diff = (self.y - other.y) ** 2
# return (x_diff + y_diff) ** .5
# c = Coordinate(3, 5)
# zero = Coordinate(0, 0)
# print(c.distance(zero))
# print(c.distance(c))
# print(Coordinate.distance(c, zero))
# Getter and setter methods
# class Animal():
# def __init__(self, age):
# self.age = age
# self.name = None
# # Getters method
# def get_age(self):
# return self.age
# def get_name(self):
# return self.name
# # Setters Method
# def set_age(self,age):
# self.age = age
# def set_name(self,name):
# self.name = name
# # Speacial method : print(some description of the function)
# def __str__(self):
# return "ِAnimal: " + str(self.name) + " : " + str(self.age)
# cat = Animal(5)
# print(cat.age) # allowed but not recommended
# print(cat.get_age()) # recommended
# print(cat.name)
# cat.set_age(10)
# cat.set_name("Tom")
# print(cat.get_age())
# print(cat.get_name())
# print(cat)
# ## Inheritance
# class Animal():
# def __init__(self, age):
# self.age = age
# self.name = None
# # Getters method
# def get_age(self):
# return self.age
# def get_name(self):
# return self.name
# # Setters Method
# def set_age(self,age):
# self.age = age
# def set_name(self,name):
# self.name = name
# # Speacial method : print(some description of the function)
# def __str__(self):
# return "ِAnimal: " + str(self.name) + " : " + str(self.age)
# class Cat(Animal):
# def speak(self):
# print("Mewo")
# def say_your_type(self):
# print("I'm a cat")
# tom = Cat(2)
# print(tom.age)
# print(tom.get_age())
# tom.set_name("Tom")
# print(tom.get_name())
# tom.say_your_type()
# tom.speak()
# print(tom)
## Mathod overloading and overriding
## Overloading
## Wrong way
# def add(a, b):
# return a + b
# def add(a, b, c):
# return a + b + c
## right way
# def add(a, b, c = 0):
# return a + b + c
# print(add(5, 6, 7))
## Overriding
# class Animal():
# def __init__(self, age):
# self.age = age
# self.name = None
# # Getters method
# def get_age(self):
# return self.age
# def get_name(self):
# return self.name
# # Setters Method
# def set_age(self,age):
# self.age = age
# def set_name(self,name):
# self.name = name
# # Speacial method : print(some description of the function)
# def __str__(self):
# return "ِAnimal: " + str(self.name) + " : " + str(self.age)
# class Cat(Animal):
# # def __init__(self, name, age):
# # Animal.__init__(self, age)
# # self.name = name
# def speak(self):
# print("Mewo")
# def say_your_type(self):
# print("I'm a cat")
# def __str__(self):
# return "CAT: " + str(self.name) + " : " + str(self.age)
# tom = Cat(15)
# print(tom)
## tom = Cat("Tom", 2)
## print(tom)
# class Person():
# num_setting_name = 0
# def __init__(self, name):
# self.name = name
# def set_name(self, name):
# self.name = name
# Person.num_setting_name += 1
# def get_num_of_setting(self):
# return Person.num_setting_name
# hossam = Person("Hossam")
# print(hossam.get_num_of_setting())
# hossam.set_name("Asaad")
# print(hossam.get_num_of_setting())
# hossam.set_name("Asaad")
# print(hossam.get_num_of_setting())