🎓 A1Learner
🔍

Python Class & Static Methods

A class method is a method that receives the class itself as cls. A static method is a plain function stored inside a class.

A classmethod builds an object

from_string is an alternative constructor. It reads text, then builds a Date with cls.

class Date:
    def __init__(self, day, month, year):
        self.day = day
        self.month = month
        self.year = year

    @classmethod
    def from_string(cls, text):
        day, month, year = text.split("-")
        return cls(int(day), int(month), int(year))

date = Date.from_string("22-10-2026")
print(date.day, date.month, date.year)

cls reads class attributes

The class attribute price lives on the class. The class method reads it through cls.

class Ticket:
    price = 100

    @classmethod
    def show_price(cls):
        return cls.price

print(Ticket.show_price())

A staticmethod is a plain function

is_valid takes plain values only. The class name is just its home address.

class DateCheck:
    @staticmethod
    def is_valid(day, month):
        return 1 <= day <= 31 and 1 <= month <= 12

print(DateCheck.is_valid(31, 12))
print(DateCheck.is_valid(40, 12))

The three kinds side by side

Look at the first parameter of each method. It tells you what the method receives.

class Demo:
    def instance_method(self):
        return "instance"

    @classmethod
    def class_method(cls):
        return "class"

    @staticmethod
    def static_method():
        return "static"

demo = Demo()
print(demo.instance_method(), Demo.class_method(), Demo.static_method())

Example

class Date:
    def __init__(self, day, month, year):
        if not Date.is_valid(day, month, year):
            raise ValueError("date values are out of range")
        self.day = day
        self.month = month
        self.year = year

    def __str__(self):
        return f"{self.day:02d}-{self.month:02d}-{self.year}"

    @staticmethod
    def is_valid(day, month, year):
        return 1 <= day <= 31 and 1 <= month <= 12 and year >= 1

    @classmethod
    def from_string(cls, text):
        day, month, year = text.split("-")
        return cls(int(day), int(month), int(year))

    @classmethod
    def from_stamp(cls, stamp):
        year = stamp // 10000
        month = (stamp // 100) % 100
        day = stamp % 100
        return cls(day, month, year)


first = Date.from_string("22-10-2026")
print(first)
second = Date.from_stamp(20260105)
print(second)
print(Date.is_valid(31, 12, 2026))
print(Date.is_valid(32, 12, 2026))
try:
    Date.from_string("40-01-2026")
except ValueError as error:
    print(f"ValueError: {error}")

Key points

  • Instance method: a method that receives one object as self
  • Class method: a method that receives the class itself as cls
  • Use @classmethod for alternative constructors, methods that build objects another way
  • Static method: a plain function stored inside a class, with no self and no cls
  • Use @staticmethod for helpers that need the class name as a home only
  • The validator in this lesson checks simple ranges only

In this example, Date offers two extra ways to build a date. The from_string class method splits the text into three parts. It turns each part into a number, then builds the object with cls. The from_stamp class method cuts one long number into year, month, and day. The is_valid static method checks the three values are in range. The __init__ method uses that same check before storing anything. The bad date with day 40 fails the check and raises ValueError.

Note

A class method gets the class even when you call it on an object.

It never gets that one object.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which kind of method fits an alternative constructor like from_string?