Tuesday, July 21, 2015

Object Oriented Programming with Python

Object oriented programming is the way of looking at things in hand as object. Think Object as a real life thing with certain properties and attributes. Suppose we have a list of student,we think about students -they have a name, age, class,grade etc. as their properties or attributes and some method like finding average number, finding next grade etc. So, we generalize the concept of student as class Student and every individual student is the object of that class. Lets see the python programming examples  -

class Student:
        def __init__(self,name,age):
                self.name=name
                self.age=age
                self.grades=list()
        def __str__(self):
                return "Name :%s"%(self.name)+" "+"Age:%2d"%self.age
        def set_grades(self,grade):
                self.grades.append(grade)
        def avg_grades(self):
                return float(sum(self.grades))/len(self.grades)

Look at the example above, we have defined a class Student in the first statement. Notice the colon and indentation. Like other object oriented programming language we need to or rather we should  initialize classes with their attributes. The class Student has two attributes which need to be passed when we want to create an instance of the class. So, to create an Student object we need to write like below -

a=Student("Jhon",23)

This will create a Student object with name "Jhon" and age as 23. Next we have __str__() method, which is nothing but a show value kind of method. Write str(a) to understand what it is.
Now, we have not supplied the grades any value in the initialization phase of the class. To add grades to the Student we have created we have to write -

a.set_grades(89)

a.set_grades(78)
a.set_grades(99)
a.set_grades(79)
a.set_grades(76)

The next method avg_grades() will simply sums all the grades , then take  average and returns it.
To run it, you should write something like -
a.avg_grades()

Now look at the example below - 

class Grad_Student(Student):
        def __init__(self,name,age,university):
                Student.__init__(self,name,age)
                self.university=university
        def __str__(self):
                return "Name :%s"%(self.name)+" "+"Age:%2d"%self.age+"  "\
                       +"University:%s"%self.university

       
The class above inherited from the Student class i.e. it can access to all the methods defined in the Student class. Look at the initialization process, the __init__method here takes three argument with one extra argument 'university' and then it calls the __init__ of Student class which initialized the name and age attributes of the class and the other attribute university is initialized here.

 We can use the set_grades() and avg_grades() of the Student class from the Grad_Student class.


Also look at the __str__() here. This  is the example of polymorphism. The method described here overwites the method in Student class. So when run str(a) it will run the method here. 



Please visit my website http://pythonbeginner.in for further studies and learn python effortlessly.

No comments:

Post a Comment

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...