Previous

Basic Idea about Types of Variables & Methods

Next

Instance Variables

  • If the value of a variable varies from object to object, such variables are called instance variables or object-level variables.
  • For example: name and rollno of a student.
  • For every object, a separate copy of instance variables is created.
  • Most of the time, instance variables are declared inside the constructor using the self variable.

Static Variables

  • If the value of a variable does not vary from object to object, it is not recommended to declare it as an instance variable.
  • Such variables should be declared at the class level as static variables.
  • For instance variables, a separate copy is created for each object.
  • In contrast, for static variables, only one copy is created at the class level and shared by every object of that class.
  • Most of the time, static variables should be declared directly within the class.

Local Variables

  • To meet temporary requirements, variables can sometimes be declared inside a method. Such variables are called local variables or method-level variables.
class Employee:
    company_name = "BinaryBridgeTech" # Static variable

    def __init__(self, emp_name, emp_id):
        self.emp_name = emp_name # instance variable
        self.emp_id = emp_id # instance variable

    def info(self):
        i = 5 # i is local variable
        for n in range(i): # n is local variable
            print(n)

 

Types of Methods

  1. Instance Methods
  2. Class Methods
  3. Static Methods

Instance Methods

  • Inside a method, if you are trying to access instance variables, the method refers to a particular object and should be declared as an instance method.
  • The first argument to an instance method is always self, which is a reference variable for the current object.

Class Methods

  • If a method uses only static variables and does not access any instance variables, it is not related to a particular object and is considered a class-level method. Such methods should be declared as class methods.
  • Class methods must be declared with the @classmethod decorator.
  • The first argument to a class method is always cls, which is a reference variable to the class object.
  • For each class, a special object is created by the Python Virtual Machine (PVM) to maintain class-level information, known as the class-level object. The cls variable is a reference to this object.

Static Methods

  • If a method does not use any instance variables or static variables, it is considered a general utility method and should be declared as a static method.
  • Static methods should be declared using the @staticmethod decorator.

 

Emaple :

 

class Employee:
    company_name = "BinaryBridgeTech" # Static variable

    def __init__(self, emp_name, emp_id):
        self.emp_name = emp_name # instance variable
        self.emp_id = emp_id # instance variable

    def getEmployeeInfo(self):
        print('Employee Name: ', self.emp_name)
        print('Employee Id: ', self.emp_id)

    @classmethod
    def getCompanyName(cls):
        print('Company Name: ', cls.company_name)

    @staticmethod
    def getSum(a,b):
        sum = a+b
        return  sum