Python classmethod: How to Use classmethod() Function

Python is an OOP language. So, we will see how we can define and use class methods. We will see the ways in which we can create the class methods and call them in our program.

Python classmethod()

Python classmethod() is an inbuilt function that returns the class method for the given function.  The classmethod() method returns the class method for the given function. Python classmethod() is the way to define the function of the python class.

A class method is a method that is bound to the class and not the object of the class.

They have access to the state of the class as it takes the class parameter that points to a class and not an object instance.

It can modify the class state that would apply across all the instances of the class.

For example, it can change a class variable that will apply to all the instances.

In Python, there are two types of functions that can be created in the program. They are:

  1. Static methods
  2. Class methods

Making Python class methods

There are two ways to create class methods in Python:

  1. Using classmethod(function)
  2. Using @classmethod annotation

Using @classmethod annotation

The @classmethod decorator is the builtin function decorator that is the expression that gets evaluated after your function is defined.

The result of the evaluation shadows your function definition.

See the following syntax.

class C(object):
    @classmethod
    def fun(cls, arg1, arg2, ...):
       ....
fun: function that needs to be converted into a class method
returns: a class method for function.

Using classmethod(function)

Syntax

classmethod(function)

#classmethod() Parameters

The classmethod() method takes the single parameter:

  1. function – It is a function that needs to be converted into the class method.

The classmethod() method returns the class method for the given function.

What is the class method?

The class method is the method that is bound to a class rather than its object. It doesn’t require the creation of the class instance, much like the staticmethod.

A class method can be called both by a class and its object.

Class.classmethod()
Or even
Class().classmethod()

Create a class method using classmethod()

Okay, now see the following code example.

# app.py

class App:
    price = 25

    def printPrice(cls):
        print('The price is:', cls.price)


# create printPrice class method
App.printPrice = classmethod(App.printPrice)

App().printPrice()

See the following output.

➜  pyt python3 app.py
The price is: 15
➜  pyt

Here, we have a class App, with a member variable price assigned to 15.

We also have a function printPrice() which takes a single parameter cls and not self, as we usually take.

The cls accepts the class App as a parameter rather than the App’s object/instance.

Now, we pass the method App.printPrice() as an argument to a function classmethod. It converts a method to the class method so that it accepts the first parameter as a class (i.e., App).

In the final line, we call printPrice() without creating an App object as we do for static methods. This prints the class variable price.

Why we use classmethod

  1. We generally use the class method to create factory methods. Factory methods return the class object ( similar to a constructor ) for different use cases.
  2. We generally use static methods to create utility functions.

How to define a class method and static method

If we want to define the class method in python, we use the @classmethod decorator, and to define the static method; we use the @staticmethod decorator.

Class method vs. Static Method

  1. The class method takes cls as the first parameter, while a static method needs no specific parameters.
  2. The class method can access or modify the class state while a static method can’t access or change it.
  3. In general, static methods know nothing about the class state. Static methods are utility type methods that take some parameters and work upon those parameters. On the other hand, the class methods must have class as the parameter.
  4. We use the @classmethod decorator in python to create the class method, and we use the @staticmethod decorator to create a static method in python.

Conclusion

The @classmethod annotation decorator is used to create the factory methods as they can take any input and provide the class object based on the parameters and processing.

If we use the decorator, it is possible to create as many constructors for a class which behaves as the factory constructors.

Finally, Python classmethod() Example Tutorial is over.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.