Industry Ready Java Spring Boot, React & Gen AI — Live Course
PythonOOPs Concepts

Types of Methods


Types of Methods in Python

In Python, methods define the behavior of a class and determine how data is accessed and modified. Based on what they operate on—object data, class-level data, or no data at all—Python provides three types of methods:

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

Variables and Their Scope

Before exploring method types, it is important to understand variable scope:

Class_Instance_Variables


Instance Methods

What Are Instance Methods?

  • Instance methods are the most common type of methods
  • They use self as the first parameter
  • They operate on instance variables
  • Each object maintains its own independent state

Example

class Computer:
    brand = "Telusko AI"

    def __init__(self, cpu, ram, ssd):
        print("init called")
        self.cpu = cpu
        self.ram = ram
        self.ssd = ssd

    def config(self):
        print("config :", self.cpu, self.ram, self.ssd)

com1 = Computer("i5", "16GB", "512GB")
com2 = Computer("i9", "96GB", "2TB")

com1.config()
com2.config()
print(Computer.brand)

Output

init called
init called
config : i5 16GB 512GB
config : i9 96GB 2TB
Telusko AI

Key Points

  • self refers to the current object
  • Instance methods work with object-specific data
  • Changes affect only that object

Class Methods

What Are Class Methods?

  • Defined using the @classmethod decorator
  • Use cls as the first parameter (by convention)
  • Work with class variables
  • The class itself is passed automatically, not an object

Example

class Computer:
    brand = "Telusko AI"

    def __init__(self, cpu, ram, ssd):
        self.cpu = cpu
        self.ram = ram
        self.ssd = ssd

    def config(self):
        print("config :", self.cpu, self.ram, self.ssd)

    @classmethod
    def info(cls):
        return cls.brand

print(Computer.info())

Output

Telusko AI

Key Points

  • Class methods operate on shared data
  • Modifying class variables affects all objects
  • Often used for factory methods or class-level information

Static Methods

What Are Static Methods?

  • Defined using the @staticmethod decorator
  • Do not receive self or cls
  • Independent of both object and class state
  • Used as utility or helper functions

Example

class Computer:
    @staticmethod
    def gb_to_bytes(gb):
        return gb * (1024 ** 3)

print(Computer.gb_to_bytes(16))

Output

17179869184

Key Points

  • No access to instance or class variables
  • Logically related to the class
  • Improves code organization and readability

Impact of Variable Scope

  • Modifying a class variable using the class name affects all objects
  • Modifying an instance variable using self affects only that object
  • Instance and class variables exist in separate namespaces

Method_Types


Summary

  • Python provides three types of methods: instance, class, and static.
  • Instance methods operate on object-specific (instance) data using self.
  • Class methods operate on shared class-level data using cls.
  • Static methods act as utility functions, independent of class and object state.
  • Using the appropriate method type results in cleaner, more maintainable, and well-structured object-oriented code.

Written By: Muskan Garg

How is this guide?

Last updated on