Table of Contents

What Will You Learn
In this tutorial, you'll learn how methods bring behavior to Python classes and how they differ depending on their type. You’ll explore instance methods that operate on specific objects, class methods that work with the class itself, and static methods that act independently of both. The guide explains when and why to use each method type, how to define them using the appropriate decorators, and common mistakes to avoid. With practical examples and structured explanations, you’ll gain the skills to organize your code effectively and write classes that are both powerful and easy to maintain.


Understanding methods is fundamental to mastering object-oriented programming. They define the behaviors and actions that an object can perform. Without methods, classes are just static containers of data — methods breathe life into them.

As a beginner, knowing when and how to use instance, class, and static methods helps you write cleaner, more reusable code. These tools are not just formalities; they’re core to designing scalable, modular systems. Mastering them separates procedural scripts from maintainable, professional applications. That’s why learning how methods work — and when to use each type — is not optional, but essential.

What Are the Methods in OOP in Python?

In object-oriented programming, methods are functions defined inside a class that operate on instances of that class. They allow your objects to perform actions and interact with their internal state or class-level data. In Python, methods are defined with def just like regular functions, but with at least one required argument depending on the method type.

There are three main types of methods: instance methods, class methods, and static methods. Each serves a different purpose and affects how your code behaves and scales. Below are some examples to clarify how methods are used in practice:


    # Instance method
    class Car:
        def start_engine(self):
            print("Engine started")

    my_car = Car()
    my_car.start_engine()  # Output: Engine started
  

In this example, start_engine is an instance method that operates on a specific object of the Car class. It uses self to refer to the instance calling it. Instance methods are the most common type and are used to access or modify instance-level data. They are defined with the def keyword and always take self as the first parameter. This allows them to access instance attributes and perform actions specific to that instance.


      # Class method
      class Counter:
          count = 0

          @classmethod
          def increment(cls):
              cls.count += 1

      Counter.increment()
      print(Counter.count)  # Output: 1
  

Here, increment is a class method that modifies a class-level attribute count. It uses the @classmethod decorator and takes cls as its first parameter, which refers to the class itself, not an instance. Class methods are useful for operations that affect the class as a whole, such as factory methods or shared state management.


    # Static method
    class Math:
        @staticmethod
        def add(a, b):
            return a + b

    print(Math.add(3, 4))  # Output: 7
  

In this example, add is a static method that performs a utility function without needing access to instance or class data. It uses the @staticmethod decorator and does not take self or cls. Static methods are useful for grouping related functions within a class without requiring an instance. They help keep your code organized and encapsulated, especially when the method logic is independent of class or instance state.

Types of Methods in Python

Python supports three types of methods inside classes: instance methods, class methods, and static methods. Each has its own role and expected use case. Understanding their differences is key to structuring your classes properly.

Method Type Description Note
Instance Method Works with the instance (object) of the class using self. Used for accessing or modifying instance-level data.
Class Method Works with the class itself using cls. Useful for factory methods and class-wide operations.
Static Method Does not use self or cls. Used when the method logic is independent of class/instance.

Instance Methods

Instance methods are the most common type of method in Python's object-oriented programming. They are defined using the def keyword and always take self as the first parameter, which refers to the specific object calling the method. This allows access to instance-level data and behavior. Instance methods should be used when your logic depends on the current state of a specific object. They are essential when designing classes that represent real-world entities with unique data.


    class Person:
        def __init__(self, name):
            self.name = name

        def greet(self):
            return f"Hello, my name is {self.name}"

    p = Person("Alice")
    print(p.greet())  # Output: Hello, my name is Alice
  

In this example, greet is an instance method that uses self to access the name attribute of the specific Person object. It returns a personalized greeting based on the object's state. Instance methods are typically used for actions that are specific to the instance, such as updating attributes, performing calculations based on instance data, or interacting with other objects.


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

        def deposit(self, amount):
            self.balance += amount
            return self.balance

    acc = BankAccount(100)
    print(acc.deposit(50))  # Output: 150
  

Here, the deposit method modifies the balance attribute of the specific BankAccount instance. It demonstrates how instance methods can encapsulate behaviors that directly affect the object's state. Instance methods are crucial for implementing the core functionality of your classes, allowing them to interact with their own data and perform actions that are relevant to that specific instance.

When to Use Instance Methods?

Use instance methods whenever you need to access or modify the specific data tied to an individual object. These methods are tightly connected to the object's internal state and usually reflect behaviors that vary between instances.

Here are five common cases when instance methods are the right choice:

  • Working with instance attributes: When you want to read or change values stored in self.
  • Performing object-specific actions: Logic that only applies to one object at a time should live here.
  • Encapsulating internal logic: Keep the details hidden inside methods to follow the principles of OOP.
  • Using object state as context: If a method depends on current values inside the object, it must be an instance method.
  • Customizing string representations or comparisons: Override special methods like __str__() or __eq__() using self.

Class Methods

Class methods operate on the class itself rather than on instances. They take cls as their first parameter, which refers to the class—not the object. Class methods are defined using the @classmethod decorator. They are typically used when you need to maintain or initialize class-level state. These methods are ideal for creating factory methods and managing data shared across all instances.


    class Product:
        tax_rate = 0.2

        @classmethod
        def set_tax_rate(cls, rate):
            cls.tax_rate = rate

    Product.set_tax_rate(0.18)
    print(Product.tax_rate)  # Output: 0.18
  

In this example, set_tax_rate is a class method that modifies the class-level attribute tax_rate. It uses cls to refer to the class itself, allowing it to change shared data that affects all instances of Product. Class methods are useful for operations that need to affect the class as a whole, such as maintaining counters, managing shared resources, or providing alternative constructors.


    class User:
        def __init__(self, username):
            self.username = username

        @classmethod
        def from_email(cls, email):
            username = email.split("@")[0]
            return cls(username)

    u = User.from_email("This email address is being protected from spambots. You need JavaScript enabled to view it.")
    print(u.username)  # Output: admin
  

Here, from_email is a class method that creates a new User instance based on an email address. It extracts the username from the email and returns a new instance of the class. Class methods are particularly useful for alternative constructors, allowing you to create instances in different ways while keeping the logic encapsulated within the class.

When to Use Class Methods

Class methods are useful when logic is not tied to a specific instance but to the class as a whole. They're especially powerful for alternative constructors and shared configuration.

Here are five key situations where class methods are the best fit:

  • Alternative object creation: When you want to create instances using custom logic (e.g., from a string or database row).
  • Modifying class-level attributes: Update variables that belong to the class rather than a specific object.
  • Centralized configuration logic: Maintain global settings that apply across all instances.
  • Utility methods that affect the whole class: Operate on class state and structure, not on object state.
  • Tracking instance creation: Use class methods to implement registries or object counters.

Static Methods

In Python, a static method is defined inside a class but doesn’t rely on either instance (self) or class (cls) context. It behaves like a regular function but is placed within a class for logical grouping. You define a static method using the @staticmethod decorator. It’s useful when you want a function that logically belongs to the class, but doesn't need to access class or instance data. Static methods help keep related utilities bundled with the class, which improves code organization.


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

  print(MathUtils.add(5, 3))  # Output: 8

In this example, add is a static method that performs a simple addition operation. It does not require access to any instance or class data, making it suitable for static context. Static methods are often used for utility functions that are related to the class but do not need to operate on instance or class-level data.


  class TemperatureConverter:
    @staticmethod
    def celsius_to_fahrenheit(c):
        return (c * 9/5) + 32

  print(TemperatureConverter.celsius_to_fahrenheit(25))  # Output: 77.0

Here, celsius_to_fahrenheit is a static method that converts a temperature from Celsius to Fahrenheit. It does not depend on any instance or class data, making it a perfect candidate for a static method. Static methods are useful for grouping related utility functions within a class without requiring an instance.

When to Use Static Methods

Use static methods when your logic doesn’t depend on instance or class-level data. They’re perfect for keeping related utility functions inside the class scope without cluttering instance logic.

  • Helper utilities
    Group utility functions that logically belong to the class, like unit conversions.
  • Avoid unnecessary object creation
    When no class or instance state is needed, static methods avoid unnecessary instantiation.
  • Improve code organization
    Keep utility logic in context instead of spreading it across unrelated modules.
  • Enforce structure
    Static methods make it clear that a method does not affect object state, increasing code clarity.
  • Use in factory logic
    Sometimes used in conjunction with class methods to assist in object construction steps.

When to Use Which Python Method?

Choosing the right type of method in Python—instance, class, or static—is critical for writing maintainable and clean object-oriented code. Each method type has its own role and specific use case. Beginners often misuse these by defaulting to instance methods even when class or static would be more appropriate. Understanding when to use each ensures better structure and logic in your application. Below are simple, practical guidelines that experienced developers rely on when working with OOP in Python.

  • Use an instance method when your logic needs to access or modify object-level data. These are the most common and operate on specific instances of the class.
  • Use a class method when you need to work with class-level data or create alternative constructors that operate on the class itself, not individual objects.
  • Use a static method for utility or helper logic that fits conceptually within a class but doesn’t depend on instance or class data.
  • Use class methods to encapsulate logic related to the type or identity of the class, such as maintaining counters or accessing shared resources.
  • Use instance methods when encapsulating behavior that’s specific to the state of an object, such as updating a user’s profile or checking account balance.

Common Beginner Mistakes

1. Forgetting to Include self in Instance Methods

One of the most common mistakes beginners make is forgetting to include self as the first parameter in instance methods. Python relies on self to pass the current instance of the class to the method. Omitting it results in a TypeError, since Python automatically sends the instance, but the method signature isn’t prepared to receive it.


    class Person:
    def greet():
        print("Hello!")

# This will raise: TypeError: greet() takes 0 positional arguments but 1 was given
Person().greet()

Fix: Always include self as the first parameter in an instance method.


  class Person:
    def greet(self):
        print("Hello!")

Person().greet()  # Correct usage

2. Using self in Static Methods

Static methods do not have access to the instance or class. Beginners sometimes mistakenly include self in static methods, assuming it works like an instance method. This misleads both the interpreter and anyone reading the code.


  class Tools:
    @staticmethod
    def show(self):
        print("This won't work correctly.")
      

Fix: Remove self from the static method’s parameters.


  class Tools:
    @staticmethod
    def show():
        print("This works as expected.")

3. Forgetting @classmethod or @staticmethod Decorator

Another frequent error is defining a class or static method without using the proper decorator. Without @classmethod, the method won’t receive cls, and without @staticmethod, Python will attempt to pass the instance as the first argument.


  class Config:
    def get_version(cls):
        print("v1.0")

Config.get_version()  # Raises TypeError

Fix: Always decorate appropriately based on method type.


  class Config:
    @classmethod
    def get_version(cls):
        print("v1.0")

Config.get_version()

4. Misusing Class Methods Instead of Instance Methods

Class methods are sometimes misused where instance methods are needed. Beginners may choose @classmethod simply because they see examples online but misunderstand the context. This often leads to code that behaves incorrectly or makes it harder to manage object state.


  class User:
    name = ""

    @classmethod
    def set_name(cls, value):
        cls.name = value

User().set_name("Alice")
print(User.name)  # Affects all users

Fix: Use an instance method when managing individual object state.


  class User:
    def __init__(self):
        self.name = ""

    def set_name(self, value):
        self.name = value
      

5. Not Understanding Which Method Type to Use

Beginners often struggle to choose the right method type. They might define all methods as instance methods or misuse static methods for logic that actually needs instance or class context. This results in cluttered code and poor encapsulation.


  class Calculator:
    def multiply(a, b):
        return a * b  # Will fail if called from instance

Fix: Ask: does the method need access to the instance (self)? The class (cls)? Or neither? Choose accordingly.


  class Calculator:
    @staticmethod
    def multiply(a, b):
        return a * b
      

Frequently Asked Questions

What is the OOP method in Python?

In Python, an OOP method refers to a function defined within a class that operates on instances of that class or the class itself. There are three main types: instance methods, class methods, and static methods. Each plays a different role in object-oriented design.

Instance methods have access to instance data via self and are the most common. Class methods, defined with @classmethod, use cls to access or modify class-level data. Static methods, defined with @staticmethod, don’t access class or instance data and behave like regular functions inside the class.

These methods are essential for building reusable, modular, and encapsulated logic. Understanding how and when to use each one is a foundational skill for writing maintainable object-oriented code in Python.

How many methods are in OOPs?

In Python’s object-oriented programming model, there are primarily three types of methods: instance methods, class methods, and static methods. Each type is defined using different decorators and serves different purposes in a class.

Instance methods are the most commonly used and work with object-level data via self. Class methods are marked with @classmethod and receive the class itself as cls. Static methods are marked with @staticmethod and do not take self or cls; they are used for utility logic.

Apart from these, special methods like __init__, __str__, and __repr__ are also part of OOP but serve different structural purposes. Knowing the distinction between all these helps in building well-structured object-oriented designs.

What are the best practices for naming methods in Python?

Naming methods clearly and consistently is crucial for code readability and maintainability. Python follows PEP 8, the official style guide, which recommends using lowercase words separated by underscores for method names.

Use descriptive names that clearly explain what the method does. For instance, calculate_total() is better than calc(). Avoid abbreviations unless they are commonly understood. Start method names with a verb to indicate action, and avoid using single-character names unless in a loop or mathematical context.

Private or internal methods should start with a single underscore (e.g., _helper_function). Avoid using dunder (double underscore) names unless you are implementing Python's special methods. Consistency and clarity should guide every naming decision.

How do you call a method in OOP Python?

To call a method in Python, you use dot notation on an object or class, depending on the method type. Instance methods are called on objects, while class and static methods are called on the class itself.

For example, if car is an object of a Vehicle class with a method start(), you call it using car.start(). If there's a class method like @classmethod def from_string(cls, data):, you use Vehicle.from_string("..."). Similarly, for static methods: Vehicle.utility_function().

Python automatically passes the self argument to instance methods and cls to class methods when they are called using dot syntax. Make sure to respect the intended context — instance, class, or utility — when invoking any method.