Python classmethod()

Classmethod()

Python classmethod() is a built-in function of the Python standard library!

There are three types of methods in Python:

Here, in this article, we’re going to discuss and focus on the class method of Python. So let’s get started.


What is a Python classmethod()?

A Python classmethod() can be called by a class as well as by an object. A method that returns a class method for a given function is called a classmethod().

It is a method that is shared among all objects. It is an in-built function in the Python programming language.

A classmethod() takes only one single parameter called function. Hence, the syntax of the classmethod() method in python is as follows:

Syntax : classmethod(function)

Using the Python classmethod()

Let’s take a look at an example of the Python classmethod() function here. In the below example, we use the classmethod() function to print the marks of the student that are declared within the class instead of passing the variable to the function.

class Student:
    marks = 50

    def printMarks(cls):
        print("Marks obtained by the Student is:",cls.marks)

Student.printMarks=classmethod(Student.printMarks)
Student.printMarks()

Output:

============= RESTART: C:/Users/Admin/Student.py =============
Marks obtained by the Student is: 50

In the above code, we observe that:

  • We have a class Student with member variable marks.
  • We have a function printMarks which accepts parameter cls.
  • We have passed the classmethod() that is always attached to a class.
  • Here, the first argument is always a class itself which is why we use ‘cls’ when we define the class.
  • Then we pass the method Student.printMarks as an argument to classmethod( ). The finally printMarks is called that prints the class variable marks.

Using the Decorator @classmethod

Another method to use the classmethod( ) function is by using the @classmethod Decorator.

Instead of the object, by using the decorator method we can call the class name. It can be applied to any method of the class.

The syntax of the @classmethod Decorator method is as follows: It takes one parameter and multiple arguments.

class c(object):
    @classmethod
    def function(cls, argument1, argument2, ...):
    /*remaining code*/

Let’s see how we can run a similar example with the use of the decoartor classmethod instead of using the function as in the previous example.

class Student:
    course = 'Python'

    @classmethod
    def printCourse(cls):
        print("Course opted by the Student is:",cls.course)

Student.printCourse()

Output :

============= RESTART: C:/Users/Admin/Student.py =============
Course opted by the Student is: Python

In the example mentioned above, the @classmethod decorator has applied to the printCourse( ) method. It has one parameter cls. The method then automatically picks the member variable of the class instead of requiring the user to pass one to the function.


Conclusion

This was in brief about the two ways in which you can use the Python classmethod( ) function. Stay tuned for more articles on Python!