β‘ Python OOP β Static Methods
Introduction π
A Static Method is a method inside a class that does NOT operate on class attributes or instance attributes. It behaves like a normal function but belongs to the class for better structure and organization.
Note
π‘ Static methods do NOT take self or cls. They are used when your logic is related to the class but does not need access to class or instance data.
1. Defining a Static Method π§±
basic_staticmethod.py
class MathTools:
@staticmethod
def add(a, b):
return a + b
print(MathTools.add(5, 10))β Uses @staticmethod decorator
β No self or cls needed
2. Calling Static Methods Using Object or Class π
call_staticmethod.py
class Demo:
@staticmethod
def show():
print("Static method called!")
Demo.show() # recommended
d = Demo()
d.show() # also worksNote
β Static methods do NOT depend on object state.
3. When Should You Use a Static Method? π―
- β When the method does NOT use self or cls
- β When you want to keep utility functions inside a class
- β When logic is related to the class but not dependent on instance/class data
- β For helper, utility, or calculation methods
4. Static Method vs Class Method vs Instance Method βοΈ
| Type | First Parameter | Access | Best For |
|---|---|---|---|
| Instance Method | self | Instance attributes | Object-specific behavior |
| Class Method | cls | Class attributes | Alternative constructors, shared state |
| Static Method | No params | No access to class/instance data | Utility functions |
5. Static Method Example β Utility Calculator π’
utility_calc.py
class MathUtils:
@staticmethod
def square(n):
return n * n
@staticmethod
def cube(n):
return n ** 3
print(MathUtils.square(4))
print(MathUtils.cube(3))6. Static Method Inside a Class with Attributes π§
static_with_class.py
class Employee:
company = "Google"
@staticmethod
def greet():
print("Welcome to the company!")
Employee.greet()β Static method does not use company β so it remains independent.
7. Real-World Example: Validation Function βοΈ
validation_static.py
class Validator:
@staticmethod
def is_email(text):
return "@" in text and "." in text
print(Validator.is_email("test@gmail.com"))
print(Validator.is_email("wrongmail"))β Useful for form validation, input checking, etc.
8. Real-World Example: Geometry Class π
geometry_example.py
class Geometry:
@staticmethod
def area_circle(r):
return 3.14 * r * r
@staticmethod
def area_square(s):
return s * s
print(Geometry.area_circle(5))
print(Geometry.area_square(4))9. Static Methods for Code Organization π¦
organization_example.py
class Utils:
@staticmethod
def capitalize(text):
return text.capitalize()
@staticmethod
def reverse(text):
return text[::-1]
print(Utils.capitalize("python"))
print(Utils.reverse("hello"))β Keeps related helper functions neatly grouped inside a class.
10. Static Methods in Large Applications π
- β Logging helpers
- β Formatters
- β Converters (string β number, etc.)
- β Mathematical calculations
- β Input validators
- β Shared utilities across modules
Conclusion π
>>βStatic Methods are simple, powerful tools that help organize utility logic inside classes without depending on object data.β β¨
You now fully understand Static Methods in Python! Want the next topic? Try Constructors, Inheritance, Encapsulation, Polymorphism, or Magic Methods. Just tell me! π