PYTHON

OOPS: Class & Object


Class

  •  In Python everything is an object. To create objects we required some Model or Plan or Blueprint, which is nothing but class.
  • We can write a class to represent properties (attributes) and actions (behavior) of object.
  • Properties can be represented by variables
  • Actions can be represented by Methods.
  • Hence class contains both variables and methods.
  • We can define a class by using class keyword.
  • Within the Python class we can represent operation by using methods.
  • Within the Python class we can represent data by using variables.

syntax:

class ClassName:
    ''' doc string '''
    variables
    methods

 


 

Object

  • Physical existence of a class is nothing but object.
  • We can create any number of objects for a class.

Syntax: 

referencevariable = classname()
  • Reference variable: The variable which can be used to refer object is called reference variable. By using reference variable, we can access properties and methods of object.

 

constructor

  • Constructor is a special method in python.
  • The name of the constructor should be __init__(self)
  • Constructor will be executed automatically at the time of object creation.
  • The main purpose of constructor is to declare and initialize instance variables.
  • Per object constructor will be executed only once.
  • Constructor can take at least one argument(self)
  • Constructor is optional and if we are not providing any constructor then python will provide default constructor.

 

self

  •  self is the default variable which is always pointing to current.
  • By using self we can access instance variables and instance methods of object.
  1. self should be first parameter inside constructor
    def __init__(self):
  2. self should be first parameter inside instance methods
    def m1(self)

 

Method vs Constructor

Method

Constructor

Inside method generally we write our business logicPurpose of a constructor is to initialize an object. So we write code accordingly.
Name of method can be any nameconstructor name should be always __init__
Method won't be called automaticallyConstructor will be called automatically at the time of object initialization
Per object, method can be called any no. of times.Per object contructor will be executed only once

 


 

Type of Variable

  1. Instance Variables (Object Level Variables)
  2. Static Variables (Class Level Variables)
  3. Local variables (Method Level Variables

 

❑ Instance Variable

  • If the value of a variable is varied from object to object, then such type of variables are called instance variables.
  • For every object a separate copy of instance variables will be created.
  • Instance variable can be declare inside
  1. Inside Constructor by using self variable
  2. Inside Instance Method by using self variable
  3. Outside of the class by using object reference variable.

 

Access Instance Variable

We can access instance variables within the class by using self variable and outside of the class by using object reference.


Delete Instance Variabel
 Within a class we can delete instance variable
    del self.variableName
From outside of class we can delete instance variables
    del objectreference.variableName

 

❑ Static Variable

  • If the value of a variable is not varied from object to object, such type of variables we have to declare within the class directly but outside of methods. Such type of variables are called Static variables.
  • For total class only one copy of static variable will be created and shared by all objects of that class.
  • We can access static variables either by class name or by object reference. But recommended to use class name.


Declaration of Static Variable

  1. In general we can declare within the class directly but from out side of any method
  2. Inside constructor by using class name
  3.  Inside instance method by using class name
  4. Inside classmethod by using either class name or cls variable
  5. Inside static method by using class name


Access Static Variable

  1.  inside constructor: by using either self or classname
  2. inside instance method: by using either self or classname
  3. inside class method: by using either cls variable or classname
  4. inside static method: by using classname
  5. From outside of class: by using either object reference or classname


Modify Instance Variable

Anywhere either with in the class or outside of class we can modify by using classname. But inside class method, by using cls variable.

 

❑ Local Variable

  •  Sometimes to meet temporary requirements ofprogrammer,  we can declare variables inside a method directly. such type of variables are called local variable or temporary variables.
  • Local variables will be created at the time of method execution and destroyed once method completes.
  • Local variables of a method cannot be accessed from outside of method.

 


 

Types of Methods

  1. Instance methods
  2. Static methods
  3. Class methods

 

❑ Instance Method

  • Inside method implementation if we are using instance variables then such type of methods are called instance methods.
  • Inside instance method declaration, we have to pass self variable.
    def m1(self):
  • Within the class we can call instance method by using self variable and from outside of the class we can call by using object reference.

❖ Setter Method:

 setter methods can be used to set values to the instance variables. setter methods also known as mutator methods.


❖ Getter Method:

Getter methods can be used to get values of the instance variables. Getter methods also known as accessor methods.


❑ Static Method

  • In general these methods are general utility methods.
  • Inside these methods we won't use any instance or class variables.
  • Here we won't provide self or cls arguments at the time of declaration.
  • We can declare static method explicitly by using @staticmethod decorator
  • We can access static methods by using classname or object reference
Note:
• In general we use only instance and static methods.
• Inside static method we can access class level variables by using class name.
• class methods are most rarely used methods in python.

 



Inner class

  • Sometimes we can declare a class inside another class,such type of classes are called inner classes.
  • Without existing one type of object if there is no chance of existing another type of object, then we should go for inner classes.
    Example:
    Without existing Car object there is no chance of existing Engine object. Hence Engine class should be part of Car class.
class Car:
    class Engine:
        ...

 

Example:
Without existing university object there is no chance of existing Department object

class University:
    class Department:
        ...

 

 

Destructor

  • Destructor is a special method and the name should be __del__
  • Just before destroying an object Garbage Collector always calls destructor to perform clean up
  • Activities (Resource deallocation activities like close database connection etc).
  • Once destructor execution completed then Garbage Collector automatically destroys that object.
Note:
The job of destructor is not to destroy object and it is just to perform clean up activities.
If the object does not contain any reference variable then only it is eligible fo GC. ie if the reference count is zero then only object eligible for GC.

 


PYTHON