Python classmethod() with Examples

Python classmethod() is a built-in Python standard library function.

Python methods are classified into three types:

  • Static Method
  • Instance Method
  • Class Method

Static Method vs. Class Method

  1. A class method requires class as its initial parameter, but a static method does not.
  2. A static method cannot access or modify the class state, but a class method can.
  3. In general, static methods have no knowledge of the class state. They are utility methods that take some parameters and work with them. Class methods, on the other hand, must take the class as a parameter.
  4. In Python, we use the @classmethod decorator to create a class method and the @staticmethod decorator to create a static method.

Let us explore and concentrate on Python’s class method.

classmethod() in Python:

Python’s classmethod() function returns the class method for a given function. It is a built-in method in python.

A Python classmethod() can be invoked by both a class and an object. It is a method that all things have in common. It is a built-in feature of the Python programming language.

Syntax:

classmethod(function)

Parameters: This function takes the function name as an argument.

Return Value:

The converted class method is returned by the classmethod().

Approach:

  • Create a class say, Employee.
  • Inside the class, take a variable and initialize it with some random number.
  • Create a function/method say Getsalary() which accepts the class object as an argument.
  • Print the class variables by using the class object.
  • Pass the Getsalary() method to the classmethod() function and store it as the same function(Employee.Getsalary).
  • Call the above function.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employee
class Employee:
    # Inside the class, take a variable and initialize it with some random number.
    salary = 30000
    # Create a function/method say Getsalary() which accepts the class
    # object as an argument

    def Getsalary(cls_obj):
        # Print the class variables by using the class object.
        print("The salary of an Employee = ", cls_obj.salary)


# Pass the Getsalary() method to the classmethod() function and store it as the
# same function(Employee.Getsalary).
Employee.Getsalary = classmethod(Employee.Getsalary)
# Call the above function.
Employee.Getsalary()

Output:

The salary of an Employee = 30000
@classmethod Decorator:

The @classmethod decorator is a built-in function decorator that is an expression that is evaluated after you create your function. The outcome of that evaluation shadows your function definition.

The class is sent as the implicit first argument to a class method, just as the instance is passed to an instance method.

We can invoke the class name instead of the object by utilizing the decorator method. It can be applied to any of the class’s methods.

Syntax:

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

function: It is the function to be converted into a class method.
Return Value: A class method for the function is returned.

  • A class method is a method that is linked to the class rather than the class’s object.
  • They have access to the class’s state since it takes a class parameter that points to the class rather than the object instance.
  • It has the ability to change a class state that applies to all instances of the class. It can, for example, change a class variable that applies to all instances.

For Example:

Approach:

  • Create a class say, Employee.
  • Inside the class, take a variable and initialize it with some random number.
  • Apply the @classmethod annotation for the function to access the function outside the class instead of creating an object.
  • Create a function/method say Getsalary() which accepts the class object as an argument.
  • Print the class variables by using the class object.
  • Call the above function with the class name.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employee
class Employee:
    # Inside the class, take a variable and initialize it with some random number.
    salary = 30000

    # Apply the @classmethod annotation for the function to access the function
    # outside the class instead of creating an object.
    @classmethod
    # Create a function/method say Getsalary() which accepts the class
    # object as an argument
    def Getsalary(cls_obj):
        # Print the class variables by using the class object.
        print("The salary of an Employee = ", cls_obj.salary)


# Call the above function with the class name.
Employee.Getsalary()

Output:

The salary of an Employee =  30000

The @classmethod decorator has applied to the Getsalary() function in the above example. It has a single parameter called cls_obj. The method then selects a member variable of the class automatically rather than requiring the user to pass one to the function.