All Courses
Object-Oriented Programming

Python Static Methods

What is a Static Method?

A static method is a method that lives inside a class but has no access to self or cls. It doesn't act on an instance or the class — it behaves just like a regular function, but is grouped inside a class because it's logically related to it.

class Student:
    @staticmethod
    def average_from_grades(grades):
        return sum(grades) / len(grades)

Defining a Static Method

Use the @staticmethod decorator. No mandatory first parameter (self or cls):

class Student:
    def __init__(self, name, grades=[]):
        self.name = name
        self.grades = grades

    @staticmethod
    def average_from_grades(grades):
        return sum(grades) / len(grades)

Calling a Static Method

Can be called on the class or on an instance — both work identically:

s1 = Student("Tim", [80, 75, 65, 100])
s2 = Student("Clement", [60, 50, 65, 60])

# Via the class
Student.average_from_grades(s1.grades)       # 80.0

# Via an instance (works the same)
s1.average_from_grades(s1.grades)            # 80.0

# Flexibility — pass any list you want
s1.average_from_grades(s2.grades)            # 58.75

# Combine grade lists across students
s1.average_from_grades(s1.grades + s2.grades)  # 69.375

Static Method vs Instance Method

Static Method Instance Method
Decorator @staticmethod None
First parameter None (no hidden param) self
Access to instance attributes?
Access to class attributes? ⚠️ Possible but bad practice ❌ (use class method)
Flexibility High — works like a plain function Tied to the instance

Why use a static method over an instance method? - When you want flexibility to pass custom data (e.g. a slice of grades, or combined grades from multiple students) - When the logic doesn't need to directly read instance attributes - When you want to use the method independently of any specific object


Static Attributes

Static attributes are simply another name for class attributes in Python — they are the same thing:

class Student:
    grade_bump = 2.0    # class attribute (also called a static attribute)

If you hear "static attribute" — it just means class attribute.


Full Three-Method Comparison

class Student:
    grade_bump = 2.0   # class/static attribute

    def __init__(self, name, grades=[]):
        self.name = name
        self.grades = grades

    # --- INSTANCE METHOD ---
    def average(self):
        return sum(self.grades) / len(self.grades)
        # Uses self — accesses instance attributes directly

    # --- CLASS METHOD ---
    @classmethod
    def average_from_grades_bumped(cls, grades):
        avg = cls.average_from_grades(grades)   # can call static methods
        return min(avg + cls.grade_bump, 100)   # uses class attribute via cls
        # Uses cls — accesses class attributes and other class/static methods

    # --- STATIC METHOD ---
    @staticmethod
    def average_from_grades(grades):
        return sum(grades) / len(grades)
        # No self or cls — works like a plain function

Rules & Recommended Conventions

Static methods should be truly independent: - Technically you can access class attributes inside a static method (e.g. Student.grade_bump) - But you should not — static methods are supposed to work like standalone functions - If you find yourself needing class attributes inside a static method → convert it to a @classmethod

Avoid hardcoding values that are stored as class attributes:

# ❌ Bad — ignores the class attribute
return min(avg + 2.0, 100)

# ✅ Good — uses the class attribute so changes propagate automatically
return min(avg + cls.grade_bump, 100)

When to Use Each Method Type

Use case Method type
Need access to instance data (self.name, self.grades) Instance method
Need access to class data or other class/static methods Class method (@classmethod)
Logic is self-contained, acts like a utility function Static method (@staticmethod)
Grouping related utility functions with no class/instance dependency Static method

Key Takeaways & Recap

  • @staticmethod — no hidden first parameter, no access to instance or class by default
  • Best thought of as a regular function housed inside a class for organizational purposes
  • Common use case: utility/helper functions tightly related to a class (e.g. math helpers, validators)
  • Classes can consist entirely of static methods with no __init__ at all — purely for grouping