In object-oriented programming, we use instance methods and class methods. Inside a class, we can define the following three types of methods.
- Instance method: Used to access or modify the object’s state. If a method uses instance variables, it’s an instance method. It must have a
selfparameter that refers to the current object. - Class method: Used to access or modify the class state. If a method uses only class variables, declare it as a class method. A class method has a
clsparameter that refers to the class. - Static method: A general utility method that performs a task in isolation. It doesn’t use instance or class variables, because it takes neither
selfnorcls.
Also, read Python Class method vs Static method vs Instance method.
After reading this article, you’ll learn:
- How to create and use class methods in Python
- How to create a class method using the
@classmethoddecorator and theclassmethod()function - How to use class methods as factory methods (alternative constructors)
- How class methods behave in inheritance
- How to dynamically add or delete class methods
Table of contents
What is Class Method in Python
A class method is a method that is bound to the class itself, not to a specific object. It belongs to the class level, and all instances of the class share it.
- A class method is bound to the class, not to an object of the class. Through
cls, it can access class variables, but not instance variables. - It can modify the class state by changing the value of a class variable, and the change applies to all objects of the class.
If a method uses only class variables, declare it as a class method. The first parameter of a class method is cls, which refers to the class.
Class methods are often used as factory methods. A factory method creates and returns an object of the class from a different kind of input, such as a birth year instead of an age, or a string instead of separate values. Because it uses cls instead of a hard-coded class name, it also works correctly for subclasses.
A class method can be called using ClassName.method_name() or through an object of the class.

Define Class Method
Every method you define in a class is an instance method by default. To make it a class method, you must tell Python explicitly using the @classmethod decorator or the classmethod() function.
Class methods are defined inside a class, much like a regular function.
Inside an instance method, we use self to access or modify instance variables. In the same way, inside a class method, we use cls as the first parameter to access class variables. This gives the class method control over the class state.
- You may use a different name instead of
cls, but it’s discouraged, becauseclsis the standard convention in Python. - A class method can access only class attributes, not instance attributes, because it doesn’t receive an object.
Example 1: Create Class Method Using @classmethod Decorator
To make a method a class method, add the @classmethod decorator before the method definition, and add cls as the method’s first parameter.
@classmethod is a built-in decorator. Python applies it right after the function is defined and turns the function into a class method.
Let’s see how to create a factory method using a class method. In this example, we create a Student object from a birth year instead of an age.
Output (when run in 2026)
Jessa's age is: 20 Joy's age is: 31
- In the above example, we created two objects: one using the constructor and the second using the
from_birth_year()class method. - The constructor takes two arguments,
nameandage. The class method takescls,name, andbirth_year, and returns a new object of the class. - The
@classmethoddecorator convertsfrom_birth_year()into a class method. from_birth_year()receives theStudentclass asclsand callscls(name, date.today().year - birth_year), which is equivalent toStudent(name, age).
Example 2: Create Class Method Using classmethod() function
Apart from the decorator, the built-in function classmethod() can convert a normal function into a class method. It takes a function and returns a class method.
Syntax:
classmethod(function)Code language: Python (python)
function: The function you want to convert into a class method.- It returns the converted class method.
Note: The function you want to convert must accept the class (cls) as its first argument, just as an instance method receives the instance (self).
Because a class method is bound to the class rather than an object, we can call it through both the class and an object.
The classmethod() function is the older way to create a class method. In modern Python code, use the @classmethod decorator. It’s cleaner and easier to read.
Example: Create class method using classmethod() function
Output
School Name is : ABC School
Example 3: Access Class Variables in Class Methods
Using a class method, we can access and modify class variables. Let’s see how.
Class variables are shared by all instances of a class. When a class method changes a class variable, the change applies to all objects of the class.
Output:
Jessa 20 School: ABC School Jessa 20 School: XYZ School
Example 4: Call a Class Method From an Object
You can call a class method through an object too. Python still passes the class (not the object) as cls. That’s why a class method can’t read instance variables such as name.
Output:
ABC School ABC School AttributeError: type object 'Student' has no attribute 'name'
Class Method in Inheritance
In inheritance, a child class inherits the class methods of its parent class.
Let’s create a Vehicle class with a factory class method from_price() that returns a new object with the price converted from US dollars to Indian rupees. When we call the same method using the child class name, it returns an object of the child class.
This works because cls refers to whichever class the method was called on. So a factory class method in a parent class always creates an instance of the correct derived class.
Example
Output
BMW X5 65000 BMW X5 4875000 <class '__main__.Car'>
Dynamically Add Class Method to a Class
Typically, we add class methods to the class body when defining a class. However, Python is a dynamic language that allows us to add or delete methods at runtime. This is helpful when you want to extend a class’s functionality without changing its original code.
Use the classmethod() function to add a new class method to a class.
Example:
Let’s see how to add a new class method to the Student class at runtime.
Output
Jessa 14 Below exercises for ABC School
Dynamically Delete Class Methods
We can also delete class methods from a class dynamically. In Python, there are two ways to do it:
- By using the
delstatement - By using the
delattr()function
By using the del statement
The del statement removes an attribute or method from the class. Use the del ClassName.method_name syntax to delete a class method.
Example:
In this example, we delete the class method change_school() from the Student class. If you try to call it after removing it, you’ll get an AttributeError.
Output
XYZ School AttributeError: type object 'Student' has no attribute 'change_school'
By using the delattr() function
The delattr() function deletes a named attribute or method from a class. It takes two arguments: the class (or object) and a string with the attribute or method name.
Example
Output
XYZ School AttributeError: type object 'Student' has no attribute 'change_school'
Summary
- A class method is bound to the class. Its first parameter,
cls, refers to the class, not to an object. - Create a class method with the
@classmethoddecorator (preferred) or theclassmethod()function. - Class methods can read and modify class variables, but they can’t access instance variables.
- Call a class method with
ClassName.method()orobject.method(). In both cases,clsis the class. - Class methods are ideal for factory methods (alternative constructors). Using
cls(...)makes them return the correct subclass in inheritance. - Add a class method at runtime with
ClassName.name = classmethod(func), and remove one withdelordelattr().
