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 to 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() takes single parameter

function:- classmethod function in Python accept function as a parameter.

Return Type

classmethod() returns class method for given function

Example: Create class method using classmethod()

Here we have created 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

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:- Create class method using @classmethod


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.

Difference between static method and class method

Static MethodClass Method
The static method does not take any arguments implicitly.The Class method take cls as a first argument implicitly.
Python static method does not know anything about class it only deals with parameters.The Class method bound to the class.
The static method only bound to the class but not to the object.The Class method is able to change the state of the class that apply for all the class’s instances.
The static method creates using <strong>@staticmethod</strong> decorator and staticmethod().The Class method can be call using class name and object.
A static method can be called from both class and object.The Class method creates using @classmethod and classmethod().

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

Previous Article
Next Article

Related Posts

Leave a Reply