Object Oriented Programming in Python

Object oriented Programming language goal to make the code more secure and more reusable,along with extensibility.it is a way to create the code in manageable with easy and understandable format.

Advantages of OOPS:

  1. Code Reusability : Once we write code in an Object oriented programming language we can use it many times according to our need.
  2. Security : we can you data hiding and abstraction process to prevent data to unauthorised access.
  3. Data Redundancy: It means that if the two data are stored in a database with the different fields ,we can access another by inheritance process.
  4. Code Maintainability : sWith the help of these advantages we can create high extensive and related to real world programs.

Object

An object is an entity which contains data as well as procedures that operate on the data.the data and procedures inside an object are known as attributes and methods respectively.

Class

  • A class is simply a template where we define attributes and methods of the object.
  • We create a new data type while defining a class.
  • It is important to note that a class is a specification, it defines a blueprint which does nothing by itself.
  • An Object is an instance of class.
  • The process by which an object is getting created from a class is called instantiation of a class. And we can define and instantiate multiple objects from a class based on requirement.
  • In Python, a new class can be defined with the help of class keyword followed by class name.
    ○ A class creates a new local namespace where all its attributes and methods are defined.
    ○ Every class in python is inherited from the base class object. We will talk about inheritance in detail later in this tutorial.
  • Example: Create a simple class that represents Employee of company, with no attributes and methods

    class Example:
        "Definition of Class"
        pass
    print(Example)
    print(type(Example))

    Output:

    Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
    
    >>> 
    === RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/oops.py ===
    
    
    >>>

    Creating of the instances of example class

    Example: Creating two instances of Employee class:

    class Example:
        '''Defines a Example class'''
        pass
    
    exm_1 = Example() 
    exm_2 = Example()
    print(exm_1)
    print(type(exm_1))
    print(exm_2)
    print(type(exm_2))

    Output:

    Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
    
    >>> 
    === RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/oops.py ===
    <__main__.Example object at 0x03201730>
    
    <__main__.Example object at 0x032019B8>
    
    >>>
    

    Method

    Functions are defined into the class which is called the method,method consists of the set of instructions,’’def” is used for creating the method.

    There are three type of method we can define,:

    1. instance method
    2. class method
    3. static method

    Instance Method

    The first parameter of the instance is denoted as user defined sully everywhere writes self but self are not keyword.

    self-Parameter:

    ‘self’ parameter is used to access attributes and methods that belong to the object of class.

    Example: Creating a method which gives basic and full display details of employee:

    class Employee:
        '''Defines a Employee class'''
        def basic_display(self,name,post):
            print('========Basic Detail===========')
            print('Name is',name)
            print('Post is',post)
        
        def full_display(self,name,post,monthly):
            print('========Full Detail===========')
            print('Name is',name)
            print('Post is',post)
            print('Monthly salary is Rs.',monthly)
    
    
    emp_1 = Employee() 
    emp_1.basic_display('Kamlesh', 'Manager')
    emp_1.full_display('Kamlesh', 'Manager',2000)

    Output:

    Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
    
    >>> 
    ================== RESTART: C:/Users/PC/Desktop/Hello.py ================
    ========Basic Detail===========
    Name is Kamlesh
    Post is Manager
    ========Full Detail===========
    Name is Kamlesh
    Post is Manager
    Monthly salary is Rs. 2000
    

    Member Variable

    A member variable is a variable defined inside class that holds the attributes associated with the object of class.In Python, member variable can be of two types:

    1. instance variable
    2. class variable

    Instance Variable:

    • Instance variables contain attributes which are unique to each instance of class. These variable are not shared by other objects
    • Every object of the same class will have its own copy of the instance attributes.
    • An instance variable is defined inside a method, belonging only to the current instance of a class.
    • It’s value is assigned inside a constructor or method with a self parameter.
    • General syntax for creating instance variable:
    • ○ object.variable=attribute

      Before creating instance variables, we need to understand the constructor and the self parameter.

      Constructor

      • Constructor is a special type of instance method that is called automatically whenever an object of that class is created.
      • Constructor provides initial values to instance variables of class during instantiation.

      Types of Constructors:

      1. Default constructor: The constructor which doesn’t accept any arguments is default constructor. It is definition has only an argument which is a reference to the instances being constructed.
      2. Parameterized constructor: Constructor with parameters is known as a parameterized constructor. The constructor which takes arguments is called a parameterized constructor. First argument is a reference to an instance being created as self and remaining arguments are provided by the programmer.
    • In Python, constructors are created using a special instance method name __init__.
    • Python Built in functions to list the attributes of an instance:

      Function Description
      vars() This function displays the attribute of an instance in the form of a dictionary.
      dir() Returns a list of all the members in the specified object.

      Let’s define instance variables that hold the name and monthly salary and post of each employee and initialize using constructor.

      class Employee:
         
          def __init__(self,name,month,post):
              self.name = name
              self.monthly = month
              self.post = post
          
          def basic_info(self):
              print('========Basic Detail===========')
              print('Name ',self.name)
              print('Post ',self.post)
          
          def full_info(self):
              print('========Full Detail===========')
              print('Name ',self.name)
              print('Post ',self.post)
              print('Salary',self.monthly)
      
      emp_1 = Employee("Mohan",223232,"Peon") 
      emp_2 = Employee("Geeta",232323,"Manager")
      emp_1.full_info()
      emp_2.full_info()

      Output:

      Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
      
      >>> 
      ================== RESTART: C:/Users/PC/Desktop/Hello.py ================
      ========Full Detail===========
      Name  Mohan
      Post  Peon
      Salary 223232
      ========Full Detail===========
      Name  Geeta
      Post  Manager
      Salary 232323
      >>> 
      
Subscribe Now