Class Method Decorator
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 12 of 64.
The @classmethod decorator creates methods that receive the class itself as the first parameter (cls) instead of an instance (self).
Here is an example of a class with class methods:
class Student:
school_name = "Python Academy"
student_count = 0
def __init__(self, name):
self.name = name
Student.student_count += 1
@classmethod
def get_school_info(cls):
return f"School: {cls.school_name}, Students: {cls.student_count}"
@classmethod
def create_guest_student(cls):
return cls("Guest")Call class methods using the class name:
info = Student.get_school_info()
print(info)Create some students:
alice = Student("Alice")
bob = Student("Bob")
updated_info = Student.get_school_info()
print(updated_info)Use class methods as alternative constructors:
guest = Student.create_guest_student()
print(guest.name)
print(Student.get_school_info())Output:
School: Python Academy, Students: 0
School: Python Academy, Students: 2
Guest
School: Python Academy, Students: 3Class methods can also be called from instances:
alice = Student("Alice")
info_from_instance = alice.get_school_info()
print(info_from_instance)Output:
School: Python Academy, Students: 1Key Point: Use @classmethod when you need to access class attributes or create alternative ways to construct objects. The first parameter is cls (the class itself), not self.
Cheat sheet
The @classmethod decorator creates methods that receive the class itself as the first parameter (cls) instead of an instance (self).
class Student:
school_name = "Python Academy"
student_count = 0
def __init__(self, name):
self.name = name
Student.student_count += 1
@classmethod
def get_school_info(cls):
return f"School: {cls.school_name}, Students: {cls.student_count}"
@classmethod
def create_guest_student(cls):
return cls("Guest")Call class methods using the class name:
info = Student.get_school_info()
guest = Student.create_guest_student()Class methods can also be called from instances:
alice = Student("Alice")
info_from_instance = alice.get_school_info()Use @classmethod when you need to access class attributes or create alternative ways to construct objects.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User Classes