Static methods
A static method is a method that belongs to the class but doesn’t depend on the class (like class methods) or on individual objects (like instance methods).
- Static methods don’t use
orself
.cls
- They are just regular functions that you place inside a class because they’re related to the class but don’t need to access any data from it.
Think of static methods as helpers for the class. They provide functionality that’s useful but doesn’t involve the class’s attributes or instances.
How to create a static method?
You use the @staticmethod
class MyClass:
@staticmethod
def my_static_method():
# Code for the static method
print("This is a static method!")
How are static methods different?
Static methods:
- Don’t take
(like instance methods) orself
(like class methods) as a parameter.cls
- They act like normal functions, but are grouped inside a class because they make sense conceptually as part of that class.
When to use static methods?
Use static methods when:
- The functionality is related to the class, but doesn’t need to access or modify the class’s or object’s attributes.
- You want to keep your code organized by grouping related functions inside a class.
For example, if you have a class for geometry, you might include a method to calculate the area of a circle as a static method. This calculation doesn’t depend on any specific object but is still relevant to the class.
Example: static methods in action
Here’s an example with a Calculator class:
class Calculator:
@staticmethod
def add(a, b):
return a + b
@staticmethod
def subtract(a, b):
return a - b
andadd are static methods because they don’t depend on any object or class attribute. They’re just useful tools related to the Calculator class.subtract
Using static methods:
You can call a static method using:
-
The class name:
print(Calculator.add(5, 3)) # Output: 8 print(Calculator.subtract(10, 4)) # Output: 6
-
An object of the class:
calc = Calculator() print(calc.add(7, 2)) # Output: 9
When not to use static methods?
If the method:
- Needs to interact with instance attributes → Use an instance method.
- Needs to modify or access class attributes → Use a class method.
Key points to remember:
-
What’s special about static methods?
- They don’t take self
cls
- They’re like regular functions, but placed inside a class for organization.
-
When to use?
- When the method is conceptually related to the class but doesn’t depend on its data.
-
Example scenarios:
- Utility methods (like math calculations, string formatting, or validations).
- Helper functions that don’t need to know about specific objects or the class itself.
Example: static methods with context
Let’s look at a real-world example with a Temperature Converter:
class TemperatureConverter:
@staticmethod
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
@staticmethod
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
# Use the static methods
print(TemperatureConverter.celsius_to_fahrenheit(25)) # Output: 77.0
print(TemperatureConverter.fahrenheit_to_celsius(77)) # Output: 25.0
Here, the conversion methods don’t depend on any specific temperature object. They’re just tools related to the concept of temperature conversion.