Do you think IIT Guwahati certified course can help you in your career?
No
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.
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
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
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)
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.