Table of contents
1.
Introduction
2.
Types of Methods in Python
2.1.
Instance Methods
2.2.
Python
3.
Class Methods
3.1.
For example : 
4.
Python
5.
Static Methods
5.1.
Python
6.
Comparison between Python Methods
6.1.
Access to Data
6.2.
Modification of Data
6.3.
Calling Convention
6.4.
Use Cases
7.
Here's a summary table comparing the different types of methods
8.
Frequently Asked Questions
8.1.
What is the main difference between class methods & static methods?
8.2.
Can instance methods be called without creating an object?
8.3.
Why use static methods in Python?
9.
Conclusion 
Last Updated: Jun 20, 2024
Easy

Python Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, methods are functions that belong to a class. They allow you to perform actions on objects & organize code effectively. Methods are a key aspect of object-oriented programming in Python. 

Python Method

In this article, we'll discuss the different types of methods in Python, including instance methods, class methods, & static methods. We'll also compare these methods to understand their unique characteristics & use cases.

Types of Methods in Python

Python provides three types of methods: instance methods, class methods, & static methods. Each type serves a specific purpose & has its own characteristics. Let's discuss each one of these separately: 

Instance Methods

Instance methods are the most common type of method in Python classes. They are defined within a class & can access the instance variables of the class using the self parameter. 

Example 

  • Python

Python

class MyClass:

   def __init__(self, value):

       self.value = value

  

   def instance_method(self):

       print(f"Instance method called with value: {self.value}")

obj = MyClass(42)

obj.instance_method()
You can also try this code with Online Python Compiler
Run Code


Output: 

Instance method called with value: 42


In this example, instance_method is an instance method that can access the value instance variable using self.value. Instance methods can modify the state of an object & perform actions specific to each instance of the class.

Class Methods

Class methods are methods that are bound to the class itself rather than instances of the class. They are defined using the @classmethod decorator & take the class (cls) as the first parameter instead of self. Class methods can access & modify class-level data. 

For example : 

  • Python

Python

class Student:
school_name = "ABC School" # Class variable

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

@classmethod
def change_school(cls, new_school_name):
cls.school_name = new_school_name

def display(self):
print(f"Name: {self.name}, Age: {self.age}, School: {Student.school_name}")

# Creating instances of Student class
student1 = Student("Mehak", 20)
student2 = Student("Alisha", 22)

# Display initial school name
student1.display()
student2.display()

# Changing school name using class method
Student.change_school("XYZ School")

# Display updated school name
student1.display()
student2.display()
You can also try this code with Online Python Compiler
Run Code


Output

Name: Mehak, Age: 20, School: ABC School
Name: Alisha, Age: 22, School: ABC School
Name: Mehak, Age: 20, School: XYZ School
Name: Alisha, Age: 22, School: XYZ School


In this example, change_school is a class method that changes the value of the class variable school_name. This change affects all instances of the Student class. The display method shows the updated school name for each student.

Static Methods

Static methods are methods that belong to a class but do not have access to the class or instance variables. They are defined using the @staticmethod decorator & do not take any special parameters like self or cls. Static methods are essentially standalone functions that are placed inside a class for organizational purposes. 

For example : 

  • Python

Python

class MathOperations:

@staticmethod
def add(x, y):
return x + y

@staticmethod
def subtract(x, y):
return x - y

# Using static methods without creating an instance of the class
result1 = MathOperations.add(10, 5)
result2 = MathOperations.subtract(10, 5)

print(f"Addition: {result1}")
print(f"Subtraction: {result2}")
You can also try this code with Online Python Compiler
Run Code


Output

Addition: 15
Subtraction: 5


In this example, add and subtract are static methods that perform basic mathematical operations. They don't access or modify any class or instance variables, making them independent of the class state.

Note: Static methods are useful for utility functions that don't require access to class or instance data. They can be called on the class itself without creating an instance of the class.

Comparison between Python Methods

Now, let’s compare all these methods on the basis of their usage : 

Access to Data

  • Instance Methods: Can access both instance variables (self) & class variables.
     
  • Class Methods: Can access class variables (cls) but not instance variables.
     
  • Static Methods: Cannot access either instance or class variables.

Modification of Data

  • Instance Methods: Can modify instance variables & perform actions on individual objects.
     
  • Class Methods: Can modify class variables & perform actions on the class itself.
     
  • Static Methods: Cannot modify either instance or class variables.

Calling Convention

  • Instance Methods: Called on an instance of the class using the dot notation (instance.method()).
     
  • Class Methods: Called on the class itself using the dot notation (Class.method()).
     
  • Static Methods: Called on the class itself using the dot notation (Class.method()).

Use Cases

  • Instance Methods: Used for actions specific to individual objects & accessing instance data.
     
  • Class Methods: Used for actions related to the class as a whole & accessing class-level data.
     
  • Static Methods: Used for utility functions that don't require access to class or instance data.

Here's a summary table comparing the different types of methods

Method Type Access to data Modification of data Calling Convention
Instance Method Instance and class  Instance  instance.method()
Class Method Class only  class  class.method()
Static Method None class class.method()

Frequently Asked Questions

What is the main difference between class methods & static methods?

Class methods can modify class state, while static methods cannot. Static methods are independent functions within the class.

Can instance methods be called without creating an object?

No, instance methods require an instance of the class to be called.

Why use static methods in Python?

Static methods are used for utility functions that are related to the class but do not need access to class or instance variables.

Conclusion 

In this article, we learned about the different types of methods in Python: instance methods, class methods, & static methods. We discussed their characteristics, access to data, modification abilities, & calling conventions. Understanding the differences between these methods allows you to choose the appropriate method type based on your needs & the desired behavior in your Python classes.

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass