Instance vs. Static vs. Class Methods in Python
Python is a popular programming language used by developers to create different types of applications, including web applications, scientific computing, machine learning, and more. There are different types of methods in Python which are important to understand for better programming. The three commonly used methods in Python are instance methods, static methods, and class methods. In this article, we’ll explore the differences between them.
Instance Methods
Instance methods in Python are defined inside the class, and their first parameter is always “self”, which refers to the instance of the class being used. Instance methods are used to access and manipulate the instance variables of an object.
Let’s take an example of an instance method:
“`
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_details(self):
return f”Car details: {self.make} {self.model}, {self.year} model.”
“`
In the above example, the get_details() method is an instance method. When we call this method on any object of the Car class, it will return the details of that object.
“`
car1 = Car(“Porsche”, “911”, 2021)
print(car1.get_details()) # Output: “Car details: Porsche 911, 2021 model.”
“`
Static Methods
Static methods in Python are defined inside the class, but they don’t take any instance or class arguments. They are used when a method needs to be called independently of an instance of the class.
Let’s take an example of a static method:
“`
class Calculator:
@staticmethod
def add(num1, num2):
return num1 + num2
“`
In the above example, the add() method is a static method. We can call this method without creating an instance of the Calculator class.
“`
result = Calculator.add(2, 3)
print(result) # Output: 5
“`
Class Methods
Class methods in Python are defined inside the class and their first parameter is always “cls” which refers to the class itself. They are used when a method needs to be called on the class rather than on an instance of the class.
Let’s take an example of a class method:
“`
class Shape:
sides = None
def __init__(self):
pass
@classmethod
def get_sides(cls):
return cls.sides
class Triangle(Shape):
sides = 3
class Rectangle(Shape):
sides = 4
“`
In the above example, the get_sides() method is a class method. When we call this method on any of the subclasses (Triangle or Rectangle), it will return the number of sides of that shape.
“`
print(Triangle.get_sides()) # Output: 3
print(Rectangle.get_sides()) # Output: 4
“`
Conclusion
Understanding the differences between instance, static, and class methods in Python is important for better programming. Instance methods are used to access and manipulate instance variables, static methods are used when a method needs to be called independently of an instance of the class, and class methods are used when a method needs to be called on the class rather than on an instance of the class. Knowing when and how to use each type of method will help you write more efficient and effective code in Python.