Menu Close

Python classmethod() function

In this article, you will learn all about the Python classmethod along with an example. A Python classmethod is a simple python function written inside the class and takes cls as a first parameter implicitly.

To understand Object-Oriented Programming from scratch, Then click here.

In this guide we will see total two ways to define classmethod in Python.
Two ways are:-

  • Using classmethod()
  • Using @classmethod

Python classmethod

In Python, the class method is a way to define a function for the Python class. When we create the python classmethod, then the first argument of the class method is implicitly cls. A class method can be called from both class and object.

You have to remember some points when you are working with classmethod.

  • A Python classmethod is a method that is bound to the class not the object of that class.
  • They have to access the state of the class, it takes cls as the first argument that points to the class name do not object of that class.
  • It can modify the state of the class that applies to all the instances of the class.

class method using classmethod()

In Python, classmethod() is a part of Python built-in functions that return a class method for a given function.




classmethod function in Python accept function as a parameter.

Return Type

This function returns converted class method.


Create a class method using classmethod() function.

class Person:
	name = "Programming Funda"
	def printName(cls):
		print('My name is :- ',
#call classmethod using class name
Person.printName = classmethod(Person.printName)

#call classmethod using object
p1 = Person()
Person.custom = classmethod(p1.printName)


My name is :-  Programming Funda
My name is :-  Programming Funda

In above example, printName is the class method that took cls as first parameter.

class method using @classmethod decorator

To define class method in Python, use @classmethod decorator.

Syntax of @classmethod

def func(cls, arg.....)


class Person:
	city = 'Delhi'
	def __init__(self, name, age): = name
		self.age = age
	def cityName(cls):
	def printDetails(self):
		print(f"My name is {} and my age is {self.age}")
obj = Person('Vishvajit', 20)

#call class method using class name


obj = Person('John', 30)

#call class method using object


My name is Vishvajit and my age is 20
My name is John and my age is 30

In above example, cityName is the class method that took cls as first parameter.


In this article, you have learned all about Python classmethod along with examples. a class method is a simple Python function written inside the Python class. The classmethod in Python takes cls as a first parameter.

If you like this article, please share and continue for further Python tutorials.

Python built-in functions

For more information:- Click Here

Python reversed() function
Python round() function

Was this post helpful?

Related Posts

Leave a Reply