Anyway, quality over quantity and all that so I'm going to continue my own thing.
Another thing I'd like to mention is how awesome were the midterm grades ???? I hope everyone did well on it and isn't mad at me for being all cheery about it !
Moving on ..
OOP !
Objects are data structures that contain data and can be assigned to a variable name.
Classes are basically the structure or blueprint or the base of the your program. It is used to perform actions using its methods and attributes,in a cleaner and consistent manner. For creating objects within a class we use the syntax :
post = blog(4 , '20th Feb 2015')
Here, 'post' is the variable used for the object,
'blog' is the name of the class,
'4' and '20th Feb 2015' are both attributes
Inheritance, like its name suggests allows one class, the subclass, to inherit the methods and attributes of another class, the parent class. This is done to avoid redundant and duplication of code. Simply put if there is one class that has methods and attributes in common with a few other classes, it would be more simpler to write that code only once and let it be inherited by the other classes.
For example :
class Worker:
def update_salary(self, salary):
self.total_salary = self.total_salary + salary
class HourlyWorker(Worker):
def calc_wage(self, num_hours):
self.wage = num_hours * 11.5
class SalaryWorker(Worker):
def calc_salary(self, commission):
self.salary = self.salary + (10/100)*self.salary
Here, 'Worker' is the parent class,
'HourlyWorker' and 'SalaryWorker' are both the subclasses
'update_salary' , 'calc_wage' and 'calc_salary' are methods
Both kind of workers would need their total salaries updated and so that method stays in the Parent class, however the way of calculating said salary is different and so it would be done in separate classes.
The example also shows the syntax that we use to execute inheritance.
So I hope this was simple enough to understand. I didn't try to paste actual code because I'd rather type it out here fast so I hope that didn't bother anyone.
Until next time then..
PS- Doing this during reading week took lots of coffee and a lot of motivation by my friends.
PPS - Is anyone reading this ? ...
xx