Module 4 Lesson 9: Class and Static Methods
·Programming

Module 4 Lesson 9: Class and Static Methods

Not everything needs an instance. Learn how to use @classmethod and @staticmethod for shared logic and utility functions.

Module 4 Lesson 9: Class and Static Methods

Most of the methods we’ve written so far are Instance Methods—they need self and act on a specific object (like my_dog.bark()). But sometimes, we need methods that belong to the Class itself. In this lesson, we’ll explore Class Methods and Static Methods.

Lesson Overview

In this lesson, we will cover:

  • The @classmethod Decorator: Accessing class-level data.
  • The @staticmethod Decorator: Pure utility functions inside a class.
  • Case Study: Tracking how many objects have been created.
  • Factory Methods: Using class methods to create objects in special ways.

1. Class Methods (@classmethod)

A class method receives the class itself as the first argument (called cls) instead of the object (self). This is perfect for data that is shared by EVERY object in that class.

class Student:
    total_students = 0 # This is a Class Attribute

    def __init__(self, name):
        self.name = name
        Student.total_students += 1

    @classmethod
    def get_total_students(cls):
        return f"We have {cls.total_students} students in our system."

s1 = Student("Alex")
s2 = Student("Sara")
print(Student.get_total_students()) # Output: We have 2 students...

2. Static Methods (@staticmethod)

A static method doesn't receive self OR cls. It’s just a normal function that "lives" inside a class because it's logically related to it.

class Weather:
    @staticmethod
    def celsius_to_fahrenheit(c):
        return (c * 9/5) + 32

# You don't need to create a Weather object to use this!
temp_f = Weather.celsius_to_fahrenheit(25)
print(temp_f)

3. Which One Should You Use?

  • Instance Method (self): Use when you need to change data inside a specific object (e.g., user.change_password()).
  • Class Method (cls): Use when you need to work with the class itself (e.g., counting objects or creating "Factory" functions).
  • Static Method: Use for utility functions that don't need any data from the object or the class (e.g., math conversions).

4. Why Use Them?

  1. Namespace: It keeps related code together. (A "Validator" method for a User should live inside the User class).
  2. Shared State: It’s the only way to track data across all instances (like a global high score or a total player count).

Practice Exercise: The Date Parser

Create a file named date_parser.py.

  1. Define a class DateHelpers.
  2. Add a Static Method is_valid_day(day) that returns True if a day is between 1 and 31.
  3. Add a Class Method from_string(date_str) that takes a string like "2025-12-19", splits it, and returns a message: "Year: 2025, Month: 12, Day: 19".
  4. Test both methods without creating an object of the DateHelpers class.

Quick Knowledge Check

  1. What is the difference between self and cls?
  2. What "decorator" (symbol starting with @) do you use for a Static Method?
  3. Can a Static Method access an instance attribute like self.name?
  4. Why would you use a Class Method to create a "Factory" function?

Key Takeaways

  • Instance methods are for specific objects.
  • Class methods are for the class and its shared state.
  • Static methods are for utility logic that doesn't need state.
  • Using these correctly makes your OOP design clean and professional.

What’s Next?

Our classes are getting bigger. Putting them all in one file is getting messy! In Lesson 10, we’ll learn how to Modularize OOP Projects by splitting our classes into professional folder structures!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn