Python OOPs

  Object Oriented Programming in Python 
   
Python is objected oriented programming. We can use classes and objects in Python. Class can be usderstood as a type of data structure which describes the properties and attributed of the object. Class is the blueprint of object. Object can be understood as the real world entity which has its own behaviour and state and instance of the class.

Principles of OOP -
Inheritance : In this concept we inherit the properties of one class to another. The class from which we inherit the property is called as parent class and other is child class. By inherit we means that we can use the property of one class to another. No need to write the code again.

Polymorphism : Polymorphism means many forms. In this concept we means that we can have multiple functions of same name and can be differentiate on the basis of parameters.

Encapsulation : In this concept, we encapsulate the methods and data together in a single unit. In class we encapsulate the data and methods performing on the data together.

Data Abstraction : This concept means data hiding. User need not to know about the implementation of class, he just need to use the methods and data doesn't matter how it is implemented.

Class - 
syntax: class Name:
                      #code inside class
    
E.g.: class Student:
            def __init__(self, name, age):
                    self.name = name
                    self.age = age
            def display():
                    print(self.name+" is"+self.age+" years old.")

__init__() can be understood as the contructor of class. It is called at the time of object creation. __init__ is used to define the data of the class.
self can be understood as the this keyword in OOPs. self represents the current instance of class and used with variables to show that they belongs to the current inuse instance of class.

Object -
It is the instance of class. Lets create the object of above class as - 

x = Student("Justin", 20)
x.display()        # output : Justin is 20 years old.

As we can see that we just assign the class to a variable x (here is object) with the parameter equal to the parameter in __init__ and then we can call the functions of class using the object. We can also access the variables using the object similarly as we accessed the function above.

In class it is not compulsory to have __init__ function. If we do not have __init__ in class then at the time of object creation we call the default __init__. We have two types of contructor i.e. parameterised and non-parameterised. If we have paramters in __init__ then it is parameterised other non_parameterised. 

What if we want to print the object?
We have a function in class named __str__() which is used to represent the object in string format. If we want to print the object in string format we can call that function. We also another function named __len__() which is used to represent the legth of class. Lets see how we can use them.

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
    def __str__(self):
        return f"{self.title} by {self.author}"
    def __len__(self):
        return self.pages

book1 = Book('Python', 'John', 500)
print(book1)       # output : Python by John
print(len(book1))     # output : 500

There is more to discuss about OOPs concept in Python. Lets do it in next blogs. 

Comments

Popular posts from this blog

Python Dictionary

Loops in Python