Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Aniruddh-0701 committed Nov 5, 2020
1 parent b6c0972 commit 68dd187
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 28 deletions.
52 changes: 48 additions & 4 deletions .ipynb_checkpoints/OOPS-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,55 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"## Some other methods:\n",
"\n",
"1. \\_\\_repr\\_\\_ - String representation\n",
"2. \\_\\_cmp\\_\\_ - Compare two objects\n",
"3. \\_\\_len\\_\\_ - length of object\n",
"4. \\_\\_lt\\_\\_ - less than\n",
"7. \\_\\_le\\_\\_ - less than or equal\n",
"8. \\_\\_gt\\_\\_ - greater than\n",
"9. \\_\\_ge\\_\\_ - greater than or equal\n",
"10. \\_\\_ne\\_\\_ - not equal\n",
"0. \\_\\_eq\\_\\_ - equal\n",
"5. \\_\\_getitem\\_\\_ - get a key from a iterable\n",
"6. \\_\\_setitem\\_\\_ - set a value to the given key of iterable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Private variables and methods:\n",
"\n",
"Start with \\_\\_\n",
"\n",
"E.g.: \\_\\_var, \\_\\_method\n",
"\n",
"It is advised for best programming practice to avoid calling private attributes outside the class. \n",
"But if needed, use the following syntax:\n",
"\n",
"`obj._classname__attribute`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calling a method in another method inside the class:\n",
"\n",
"Use self keyword to call the function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##"
]
}
],
"metadata": {
Expand Down
61 changes: 61 additions & 0 deletions .ipynb_checkpoints/OOPS-checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Student:
obj_count = 0
stud_list = dict()

def __init__(self, rn = 0, st_name = 'Name', *clubs): # Parametric Constructor
Student.obj_count += 1
self.roll_no = rn
self.name = st_name
self.club = clubs
Student.stud_list[rn] = st_name

def study(self):
print(f'{self.name} is Studying....')
def wake(self):
self.study()
return 'Woke'

def __del__(self): # destructor
Student.obj_count -= 1
Student.stud_list.pop(self.roll_no)

def __repr__(self):
return f'Roll no.: {self.roll_no} \nName: {self.name}\n'\
f'Clubs: {self.club}'

def __lt__(self, obj):
return self.name < obj.name
def __le__(self, obj):
return self.name <= obj.name
def __gt__(self, obj):
return self.name > obj.name
def __ge__(self, obj):
return self.name >= obj.name
def __ne__(self, obj):
return self.name != obj.name
def __eq__(self, obj):
return self.name == obj.name

def __getitem__(self, key):
return self.club[key-1]
def __setitem__(self, key, value):
self.club[key-1] = value

# getter
@property
def roll_no(self):
#print('Getter called')
return self.__roll_no

#setter
@roll_no.setter
def roll_no(self, rn):
#print('Setter called')
self.__roll_no = rn




# Object 1
st1 = Student(1, 'Prabhu', 'Coding Club', 'Robotics Club')
print(st1.wake())
58 changes: 58 additions & 0 deletions OOPS.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,64 @@
"Use \\_\\_del\\_\\_ method"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some other methods:\n",
"\n",
"1. \\_\\_repr\\_\\_ - String representation\n",
"2. \\_\\_cmp\\_\\_ - Compare two objects\n",
"3. \\_\\_len\\_\\_ - length of object\n",
"4. \\_\\_lt\\_\\_ - less than\n",
"7. \\_\\_le\\_\\_ - less than or equal\n",
"8. \\_\\_gt\\_\\_ - greater than\n",
"9. \\_\\_ge\\_\\_ - greater than or equal\n",
"10. \\_\\_ne\\_\\_ - not equal\n",
"0. \\_\\_eq\\_\\_ - equal\n",
"5. \\_\\_getitem\\_\\_ - get a key from a iterable\n",
"6. \\_\\_setitem\\_\\_ - set a value to the given key of iterable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Private variables and methods:\n",
"\n",
"Start with \\_\\_\n",
"\n",
"E.g.: \\_\\_var, \\_\\_method\n",
"\n",
"It is advised for best programming practice to avoid calling private attributes outside the class. \n",
"But if needed, use the following syntax:\n",
"\n",
"`obj._classname__attribute`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calling a method in another method inside the class:\n",
"\n",
"Use self keyword to call the function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Definining run time class attributes:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods of attributes:"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
61 changes: 37 additions & 24 deletions OOPS.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,60 @@ class Student:
obj_count = 0
stud_list = dict()

def __init__(self, rn =0, st_name = 'Name'): # Parametric Constructor
def __init__(self, rn = 0, st_name = 'Name', *clubs): # Parametric Constructor
Student.obj_count += 1
self.roll_no = rn
self.name = st_name
stud_list[rn] = st_name
self.club = clubs
Student.stud_list[rn] = st_name

def study(self):
print(f'{self.name} is Studying....')
def wake(self):
self.study()
return 'Woke'

def __del__(self): # destructor
Student.obj_count -= 1
Student.stud_list.pop(self.roll_no)

def __repr__(self):
return f'Roll no.: {self.roll_no} \nName: {self.name}\n'\
f'Clubs: {self.club}'

def __lt__(self, obj):
return self.name < obj.name
def __le__(self, obj):
return self.name <= obj.name
def __gt__(self, obj):
return self.name > obj.name
def __ge__(self, obj):
return self.name >= obj.name
def __ne__(self, obj):
return self.name != obj.name
def __eq__(self, obj):
return self.name == obj.name

def __getitem__(self, key):
return self.club[key-1]
def __setitem__(self, key, value):
self.club[key-1] = value

# getter
@property
def roll_no(self):
print('Getter called')
return f'Roll no: {self._roll_no}'
#print('Getter called')
return self.__roll_no

#setter
@roll_no.setter
def roll_no(self, rn):
print('Setter called')
self._roll_no = rn
#print('Setter called')
self.__roll_no = rn




# Object 1
st1 = Student(1, 'Prabhu')
print(st1.roll_no)
print(st1.name)

print()

#Object 2

st2 = Student(2, 'Ani')
print(st2.roll_no)
print(st2.name)
print()
print(Student.obj_count)
print()

del st2
print(Student.obj_count)

st1 = Student(1, 'Prabhu', 'Coding Club', 'Robotics Club')
print(st1.wake())

0 comments on commit 68dd187

Please sign in to comment.