Skip to content

Commit 68dd187

Browse files
committed
Update
1 parent b6c0972 commit 68dd187

File tree

4 files changed

+204
-28
lines changed

4 files changed

+204
-28
lines changed

.ipynb_checkpoints/OOPS-checkpoint.ipynb

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,55 @@
195195
]
196196
},
197197
{
198-
"cell_type": "code",
199-
"execution_count": null,
198+
"cell_type": "markdown",
200199
"metadata": {},
201-
"outputs": [],
202-
"source": []
200+
"source": [
201+
"## Some other methods:\n",
202+
"\n",
203+
"1. \\_\\_repr\\_\\_ - String representation\n",
204+
"2. \\_\\_cmp\\_\\_ - Compare two objects\n",
205+
"3. \\_\\_len\\_\\_ - length of object\n",
206+
"4. \\_\\_lt\\_\\_ - less than\n",
207+
"7. \\_\\_le\\_\\_ - less than or equal\n",
208+
"8. \\_\\_gt\\_\\_ - greater than\n",
209+
"9. \\_\\_ge\\_\\_ - greater than or equal\n",
210+
"10. \\_\\_ne\\_\\_ - not equal\n",
211+
"0. \\_\\_eq\\_\\_ - equal\n",
212+
"5. \\_\\_getitem\\_\\_ - get a key from a iterable\n",
213+
"6. \\_\\_setitem\\_\\_ - set a value to the given key of iterable"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"metadata": {},
219+
"source": [
220+
"## Private variables and methods:\n",
221+
"\n",
222+
"Start with \\_\\_\n",
223+
"\n",
224+
"E.g.: \\_\\_var, \\_\\_method\n",
225+
"\n",
226+
"It is advised for best programming practice to avoid calling private attributes outside the class. \n",
227+
"But if needed, use the following syntax:\n",
228+
"\n",
229+
"`obj._classname__attribute`"
230+
]
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"## Calling a method in another method inside the class:\n",
237+
"\n",
238+
"Use self keyword to call the function."
239+
]
240+
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"##"
246+
]
203247
}
204248
],
205249
"metadata": {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Student:
2+
obj_count = 0
3+
stud_list = dict()
4+
5+
def __init__(self, rn = 0, st_name = 'Name', *clubs): # Parametric Constructor
6+
Student.obj_count += 1
7+
self.roll_no = rn
8+
self.name = st_name
9+
self.club = clubs
10+
Student.stud_list[rn] = st_name
11+
12+
def study(self):
13+
print(f'{self.name} is Studying....')
14+
def wake(self):
15+
self.study()
16+
return 'Woke'
17+
18+
def __del__(self): # destructor
19+
Student.obj_count -= 1
20+
Student.stud_list.pop(self.roll_no)
21+
22+
def __repr__(self):
23+
return f'Roll no.: {self.roll_no} \nName: {self.name}\n'\
24+
f'Clubs: {self.club}'
25+
26+
def __lt__(self, obj):
27+
return self.name < obj.name
28+
def __le__(self, obj):
29+
return self.name <= obj.name
30+
def __gt__(self, obj):
31+
return self.name > obj.name
32+
def __ge__(self, obj):
33+
return self.name >= obj.name
34+
def __ne__(self, obj):
35+
return self.name != obj.name
36+
def __eq__(self, obj):
37+
return self.name == obj.name
38+
39+
def __getitem__(self, key):
40+
return self.club[key-1]
41+
def __setitem__(self, key, value):
42+
self.club[key-1] = value
43+
44+
# getter
45+
@property
46+
def roll_no(self):
47+
#print('Getter called')
48+
return self.__roll_no
49+
50+
#setter
51+
@roll_no.setter
52+
def roll_no(self, rn):
53+
#print('Setter called')
54+
self.__roll_no = rn
55+
56+
57+
58+
59+
# Object 1
60+
st1 = Student(1, 'Prabhu', 'Coding Club', 'Robotics Club')
61+
print(st1.wake())

OOPS.ipynb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,64 @@
194194
"Use \\_\\_del\\_\\_ method"
195195
]
196196
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {},
200+
"source": [
201+
"## Some other methods:\n",
202+
"\n",
203+
"1. \\_\\_repr\\_\\_ - String representation\n",
204+
"2. \\_\\_cmp\\_\\_ - Compare two objects\n",
205+
"3. \\_\\_len\\_\\_ - length of object\n",
206+
"4. \\_\\_lt\\_\\_ - less than\n",
207+
"7. \\_\\_le\\_\\_ - less than or equal\n",
208+
"8. \\_\\_gt\\_\\_ - greater than\n",
209+
"9. \\_\\_ge\\_\\_ - greater than or equal\n",
210+
"10. \\_\\_ne\\_\\_ - not equal\n",
211+
"0. \\_\\_eq\\_\\_ - equal\n",
212+
"5. \\_\\_getitem\\_\\_ - get a key from a iterable\n",
213+
"6. \\_\\_setitem\\_\\_ - set a value to the given key of iterable"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"metadata": {},
219+
"source": [
220+
"## Private variables and methods:\n",
221+
"\n",
222+
"Start with \\_\\_\n",
223+
"\n",
224+
"E.g.: \\_\\_var, \\_\\_method\n",
225+
"\n",
226+
"It is advised for best programming practice to avoid calling private attributes outside the class. \n",
227+
"But if needed, use the following syntax:\n",
228+
"\n",
229+
"`obj._classname__attribute`"
230+
]
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"## Calling a method in another method inside the class:\n",
237+
"\n",
238+
"Use self keyword to call the function."
239+
]
240+
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"## Definining run time class attributes:"
246+
]
247+
},
248+
{
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"## Methods of attributes:"
253+
]
254+
},
197255
{
198256
"cell_type": "code",
199257
"execution_count": null,

OOPS.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,60 @@ class Student:
22
obj_count = 0
33
stud_list = dict()
44

5-
def __init__(self, rn =0, st_name = 'Name'): # Parametric Constructor
5+
def __init__(self, rn = 0, st_name = 'Name', *clubs): # Parametric Constructor
66
Student.obj_count += 1
77
self.roll_no = rn
88
self.name = st_name
9-
stud_list[rn] = st_name
9+
self.club = clubs
10+
Student.stud_list[rn] = st_name
1011

1112
def study(self):
1213
print(f'{self.name} is Studying....')
14+
def wake(self):
15+
self.study()
16+
return 'Woke'
1317

1418
def __del__(self): # destructor
1519
Student.obj_count -= 1
20+
Student.stud_list.pop(self.roll_no)
1621

22+
def __repr__(self):
23+
return f'Roll no.: {self.roll_no} \nName: {self.name}\n'\
24+
f'Clubs: {self.club}'
25+
26+
def __lt__(self, obj):
27+
return self.name < obj.name
28+
def __le__(self, obj):
29+
return self.name <= obj.name
30+
def __gt__(self, obj):
31+
return self.name > obj.name
32+
def __ge__(self, obj):
33+
return self.name >= obj.name
34+
def __ne__(self, obj):
35+
return self.name != obj.name
36+
def __eq__(self, obj):
37+
return self.name == obj.name
38+
39+
def __getitem__(self, key):
40+
return self.club[key-1]
41+
def __setitem__(self, key, value):
42+
self.club[key-1] = value
43+
44+
# getter
1745
@property
1846
def roll_no(self):
19-
print('Getter called')
20-
return f'Roll no: {self._roll_no}'
47+
#print('Getter called')
48+
return self.__roll_no
2149

50+
#setter
2251
@roll_no.setter
2352
def roll_no(self, rn):
24-
print('Setter called')
25-
self._roll_no = rn
53+
#print('Setter called')
54+
self.__roll_no = rn
2655

2756

2857

2958

3059
# Object 1
31-
st1 = Student(1, 'Prabhu')
32-
print(st1.roll_no)
33-
print(st1.name)
34-
35-
print()
36-
37-
#Object 2
38-
39-
st2 = Student(2, 'Ani')
40-
print(st2.roll_no)
41-
print(st2.name)
42-
print()
43-
print(Student.obj_count)
44-
print()
45-
46-
del st2
47-
print(Student.obj_count)
48-
60+
st1 = Student(1, 'Prabhu', 'Coding Club', 'Robotics Club')
61+
print(st1.wake())

0 commit comments

Comments
 (0)