Menu Close

How to use Class Decorator in Python

How to use Class Decorator in Python

In this article, you will learn everything about Python class decorator and also you will learn how to use class decorator in Python with the help of the examples. Decorators in Python are one of the most important concepts that are used to change the behavior of existing code without changing the code.

Decorators in Python

Python decorator is a very powerful tool that is used to change the default behaviour of code without altering it. Decorators allow us to wrap another function to order to extend the behavior of the wrapped function.

Python provides two ways to define and use decorators first is using a function decorator and the second is using a class decorator.

Click here to know everything about function decorators in Python.

Throughout this article, we will focus only on Python class decorator.

How to declare class decorator in Python

To declare class decorator in Python, we need to use the __call__ method. Whenever a user creates an object that acts as a function then function decorators need to return an object that acts like a function.

Example


class MyDecorator:

	def __init__(self, function):
        self.function = function
    
    def __call__(self):
    
        # add some code before function call
        return self.function()
        # add some code after function call

Using class Decorator in Python

In the above example, we have seen how to declare class decorator in Python, here we are going to see how to use class decorator in Python with the help of examples.
After declaring the Python class decorator, we need to wrap it with another function using decorator.

Example


class MyDecorator:

    def __init__(self, function):
        self.function = function
    
    def __call__(self):
    
        # add some code before function call
        return self.function()
        # add some code after function call
        
        
@MyDecorator        
def func():
    print("Python is good language for Machine Learing and Data Science.")
    
func()

Output

Python is good language for Machine Learing and Data Science.

Class decorator with *args and **kwargs

We can also use a class decorator with the argument (*args) and keyword argument ( **kwargs ). We need to just pass both inside the __call__ function.

Example


class MyDecorator:

    def __init__(self, function):
        self.function = function
    
    def __call__(self, *args, **kwargs):
    
        if kwargs.get("age") >= 18:
            return self.function()
        else:
            print("You are not eligible to voting.")
        # add some code after function call
        
        
@MyDecorator        
def func():
    print("You are eligile to voting.")
    
func(age=18)

Output

You are eligile to voting.

Python class decorator with a return statement

As you can see in the above function did not return anything but sometimes function return something. let’s see how can we handle the return statement.

Example


class MyDecorator:

    def __init__(self, function):
        self.function = function
    
    def __call__(self, *args, **kwargs):
        return self.function(kwargs.get('age'))

        
        
@MyDecorator        
def func(votar):
    if votar >= 18:
        return "You are eligile to voting."
    else:
        return "You are not eligile to voting."
        
    
print(func(age=17))

Output

You are not eligile to voting.

Conclusion

So, in this article, we have seen all about How to use a class decorator in Python with the help of examples. As a Python developer, you must have knowledge of Python class decorators because it allows us to change the behavior of class and function without changing the code.

I hope this article will help you, if you like this article, please don’t forget to support and share this.

Thanks for your valuable time … 👏👏

A Comprehensive Guide to Decorators in Python
Higher-Order Function in Python

Related Posts