Types of Methods

  • Explore different types of methods in Python including instance, class, and static methods.

  • Types of Methods :

    In Python, methods are functions defined inside a class.

    1. Instance Method

    2. Class Method

    3. Static Method

    Each type serves a different purpose in real-world applications.

    1. Instance Method

    An instance method is used to access and modify object-specific data (instance variables).
    It must use self as the first parameter.

    ➡ Each object has its own copy of instance variables.

    Why use Instance Methods?

    • To work with object data

    • To represent real-life behaviors

    • To read or update instance variables

    Syntax

    class ClassName:

        def method_name(self):

            # use self.variable


    Reading Instance Data

Instance Method to Display Data

This program uses an instance method to access and display object-specific data.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display(self):
        print("Name:", self.name)
        print("Age:", self.age)

s1 = Student("Riya", 21)
s1.display()

Instance Method to Update Data

This example updates object data using an instance method.

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print("Updated Balance:", self.balance)

acc = BankAccount(5000)
acc.deposit(2000)
  • Key Points (Instance Method)

    • Uses self

    • Works with instance variables

    • Called using object

    • Most frequently used method type

    2. Class Method

    A class method works with class-level data (shared by all objects).
    It uses cls and is defined using @classmethod.

    ➡ Only one copy of class variables exists.

    Why use Class Methods?

    • To access or update class variables

    • To define factory methods

    • To perform operations related to the class as a whole

    Syntax

    class ClassName:

        @classmethod

        def method_name(cls):

            # use cls.variable

Class Method to Access Class Variable

This example shows how class data is shared across all objects.

class Company:
    company_name = "TechSoft"

    @classmethod
    def show_company(cls):
        print("Company Name:", cls.company_name)

Company.show_company()

Class Method to Update Class Data

This example updates a class variable using a class method.

class Employee:
    bonus = 5000

    @classmethod
    def update_bonus(cls, new_bonus):
        cls.bonus = new_bonus

Employee.update_bonus(8000)
print("Updated Bonus:", Employee.bonus)
  • Key Points (Class Method)

    • Uses cls

    • Works with class variables

    • Called using class name

    • Shared data for all objects

    3. Static Method

    A static method does not use instance or class data.
    It is defined using @staticmethod.

    ➡ Behaves like a normal function but belongs to a class.

    Why use Static Methods?

    • Utility/helper functions

    • Data validation

    • Logical grouping of functions

    🔹 Syntax

    class ClassName:

        @staticmethod

        def method_name():

            # utility logic

Static Method for Calculation

This static method performs addition without using class or object data.

class Calculator:
    @staticmethod
    def add(a, b):
        return a + b

print("Result:", Calculator.add(10, 20))

Static Method for Validation

This example validates age using a static method.

class User:
    @staticmethod
    def is_valid_age(age):
        return age >= 18

print(User.is_valid_age(20))
  • Key Points (Static Method)

    • No self or cls

    • No access to class or instance data

    • Used for helper logic

    • Improves code structure

    📊 Detailed Comparison Table

    Feature

    Instance Method

    Class Method

    Static Method

    Uses self

    ✅ Yes

    ❌ No

    ❌ No

    Uses cls

    ❌ No

    ✅ Yes

    ❌ No

    Access instance

    ✅ Yes

    ❌ No

    ❌ No

    Access class

    ✅ Yes

    ✅ Yes

    ❌ No

    Decorator

    ❌ No

    @classmethod

    @staticmethod

    Called using

    Object

    Class

    Class