Syntax
@staticmethod\ndef method(): ...\n\n@classmethod\ndef method(cls): ...Examples
Static Method
A utility method related to the class that does not need access to instance or class data.
class MathUtils:
@staticmethod
def is_even(n):
return n % 2 == 0
print(MathUtils.is_even(4)) # True, called directly on the class
print(MathUtils.is_even(7)) # FalseClass Method
A method that operates on the class itself rather than a specific instance.
class Employee:
count = 0
def __init__(self, name):
self.name = name
Employee.count += 1
@classmethod
def get_employee_count(cls):
return cls.count
e1 = Employee("Fola")
e2 = Employee("Zain")
print(Employee.get_employee_count()) # 2Class Method as an Alternative Constructor
A very common use of @classmethod: providing extra ways to build an instance.
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
@classmethod
def from_string(cls, date_string):
year, month, day = map(int, date_string.split("-"))
return cls(year, month, day)
def __repr__(self):
return f"{self.year}-{self.month:02d}-{self.day:02d}"
d1 = Date(2026, 3, 3)
d2 = Date.from_string("2026-07-16")
print(d1, d2)Best practices
- Use @staticmethod for helper functions logically related to the class but that need no access to self or cls
- Use @classmethod for alternative constructors (like from_string) or methods that operate on class-level data
- Prefer a module-level function instead of a @staticmethod when the logic has no real connection to the class at all
- Remember @classmethod receives cls (the class), which correctly refers to subclasses too - important when the method is inherited
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
class
The class keyword defines a blueprint for creating objects in Python. Classes are the foundation of Object-Oriented Programming (OOP), allowing you to bundle data (attributes) and functionality (methods) together. A class defines what attributes an object will have and what operations can be performed on it. Think of a class as a template or a factory for creating objects with similar characteristics.Inheritance & super()
Inheritance lets a class (the child or subclass) reuse and extend the attributes and methods of another class (the parent or superclass), written as class Child(Parent). The built-in super() function gives access to the parent class's methods from within the child, most commonly used to call the parent's __init__ so the child doesn't have to duplicate its setup logic. Python also supports multiple inheritance, where a class inherits from more than one parent.Magic (Dunder) Methods
Magic methods, also called dunder (double underscore) methods, let your custom classes hook into Python's built-in syntax and behavior. __init__ runs on object creation, __str__ controls how an object is displayed with print(), __eq__ and __lt__ control comparisons, __len__ controls the len() function, and __add__ lets objects respond to the + operator. Implementing these makes your custom classes feel like natural, first-class Python types.@property
The @property decorator lets you define a method that can be accessed like a plain attribute, without parentheses. This is useful for computed values that should look like simple attributes, and for adding validation logic that runs whenever an attribute is set, via a matching @x.setter. Properties let you start with simple public attributes and later add logic without breaking any code that uses the class.
The class keyword defines a blueprint for creating objects in Python. Classes are the foundation of Object-Oriented Programming (OOP), allowing you to bundle data (attributes) and functionality (methods) together. A class defines what attributes an object will have and what operations can be performed on it. Think of a class as a template or a factory for creating objects with similar characteristics.Inheritance & super()
Inheritance lets a class (the child or subclass) reuse and extend the attributes and methods of another class (the parent or superclass), written as class Child(Parent). The built-in super() function gives access to the parent class's methods from within the child, most commonly used to call the parent's __init__ so the child doesn't have to duplicate its setup logic. Python also supports multiple inheritance, where a class inherits from more than one parent.Magic (Dunder) Methods
Magic methods, also called dunder (double underscore) methods, let your custom classes hook into Python's built-in syntax and behavior. __init__ runs on object creation, __str__ controls how an object is displayed with print(), __eq__ and __lt__ control comparisons, __len__ controls the len() function, and __add__ lets objects respond to the + operator. Implementing these makes your custom classes feel like natural, first-class Python types.@property
The @property decorator lets you define a method that can be accessed like a plain attribute, without parentheses. This is useful for computed values that should look like simple attributes, and for adding validation logic that runs whenever an attribute is set, via a matching @x.setter. Properties let you start with simple public attributes and later add logic without breaking any code that uses the class.