PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Python Class Method Explained With Examples

Python Class Method Explained With Examples

Updated on: December 4, 2026 | 10 Comments

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 self parameter 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 cls parameter 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 self nor cls.

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 @classmethod decorator and the classmethod() 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
  • Define Class Method
    • Example 1: Create Class Method Using @classmethod Decorator
    • Example 2: Create Class Method Using classmethod() function
    • Example 3: Access Class Variables in Class Methods
    • Example 4: Call a Class Method From an Object
  • Class Method in Inheritance
  • Dynamically Add Class Method to a Class
  • Dynamically Delete Class Methods
  • Summary

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
Define class method

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, because cls is 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.

from datetime import date

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def from_birth_year(cls, name, birth_year):
        # calculate age and return a new object
        return cls(name, date.today().year - birth_year)

    def show(self):
        print(self.name + "'s age is: " + str(self.age))

jessa = Student('Jessa', 20)
jessa.show()

# create new object using the factory method
joy = Student.from_birth_year("Joy", 1995)
joy.show()
Code language: Python (python)

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, name and age. The class method takes cls, name, and birth_year, and returns a new object of the class.
  • The @classmethod decorator converts from_birth_year() into a class method.
  • from_birth_year() receives the Student class as cls and calls cls(name, date.today().year - birth_year), which is equivalent to Student(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

class School:
    # class variable
    name = 'ABC School'

    def school_name(cls):
        print('School Name is :', cls.name)

# create class method
School.school_name = classmethod(School.school_name)

# call class method
School.school_name()
Code language: Python (python)

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.

class Student:
    school_name = 'ABC School'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def change_school(cls, school_name):
        # cls.class_variable
        cls.school_name = school_name

    # instance method
    def show(self):
        print(self.name, self.age, 'School:', Student.school_name)

jessa = Student('Jessa', 20)
jessa.show()

# change school_name
Student.change_school('XYZ School')
jessa.show()Code language: Python (python)

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.

class Student:
    school_name = 'ABC School'

    def __init__(self, name):
        self.name = name

    @classmethod
    def get_school(cls):
        return cls.school_name

    @classmethod
    def show_name(cls):
        # cls has no access to instance data
        print(cls.name)

jessa = Student('Jessa')

# call using the class name
print(Student.get_school())
# call using an object: cls is still the class
print(jessa.get_school())

jessa.show_name()Code language: Python (python)

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

class Vehicle:
    brand_name = 'BMW'

    def __init__(self, name, price):
        self.name = name
        self.price = price

    @classmethod
    def from_price(cls, name, price):
        # convert price in dollars to rupees (1 USD = 75 INR)
        # create new object of the calling class
        return cls(name, (price * 75))

    def show(self):
        print(self.name, self.price)

class Car(Vehicle):
    def average(self, distance, fuel_used):
        mileage = distance / fuel_used
        print(self.name, 'Mileage', mileage)

bmw_us = Car('BMW X5', 65000)
bmw_us.show()

# class method of parent class is available to child class
# this will return the object of calling class
bmw_ind = Car.from_price('BMW X5', 65000)
bmw_ind.show()

# check type
print(type(bmw_ind))Code language: Python (python)

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.

class Student:
    school_name = 'ABC School'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def show(self):
        print(self.name, self.age)

# class ended

# method outside class
def exercises(cls):
    # access class variables
    print("Below exercises for", cls.school_name)

# Adding class method at runtime to class
Student.exercises = classmethod(exercises)

jessa = Student("Jessa", 14)
jessa.show()
# call the new method
Student.exercises()
Code language: Python (python)

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 del statement
  • 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.

class Student:
    school_name = 'ABC School'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def change_school(cls, school_name):
        cls.school_name = school_name

jessa = Student('Jessa', 20)
Student.change_school('XYZ School')
print(Student.school_name)

# delete class method
del Student.change_school

# call class method
# it will give error
Student.change_school('PQR School')
Code language: Python (python)

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

jessa = Student('Jessa', 20)
Student.change_school('XYZ School')
print(Student.school_name)

# delete class method
delattr(Student, 'change_school')

# call class method
# it will give error
Student.change_school('PQR School')
Code language: Python (python)

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 @classmethod decorator (preferred) or the classmethod() function.
  • Class methods can read and modify class variables, but they can’t access instance variables.
  • Call a class method with ClassName.method() or object.method(). In both cases, cls is 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 with del or delattr().

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

  Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com