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.

Syntax

classmethod(function)

Parameter

classmethod function in Python accept function as a parameter.

Return Type

This function returns converted class method.

Example:

Create a class method using classmethod() function.

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

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

Output

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

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

Example:

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

#call class method using class name
Person.cityName()

print("---------------")

obj = Person('John', 30)
obj.printDetails()

#call class method using object
obj.cityName()

Output

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

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

Conclusion

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