⚑ 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 works

Note

βœ” 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 βš”οΈ

TypeFirst ParameterAccessBest For
Instance MethodselfInstance attributesObject-specific behavior
Class MethodclsClass attributesAlternative constructors, shared state
Static MethodNo paramsNo access to class/instance dataUtility 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! 😊