Decorators with Parameters in Python

Decorators with Parameters in Python

In this tutorial, we will discuss Decorators with parameters in Python, but before starting this topic, users must study Decorators in Python, Function Decorators.

Decorators are a very powerful and useful tool in Python because it allows users to modify the behaviour of function or class.

Python functions can be treated as supreme citizens as objects.

  • Functions can be referenced to a variable.
  • Functions can be passed as an argument to other functions.
  • Functions can be returned from a function.

Decorators with parameters are like normal decorators.

Syntax:

Code Implementation of Decorators with Parameters:

As we execute this code, the execution will start from left to right, which will call decorator(params) for returning the function object func_obj. the func_obj(function_name) will be called by using func_obj. Inside the inner function, required operations will be performed, and the actual function reference will return for assigning to the function_name. Now, the users can use the function_name() to call the function with decorator applied.

How to Implement Decorators with Parameters:

First, we will see what we could get as output if we run a code of parameters directly without implementing any value.

Here, in the above code, as params is empty, we might get some errors.

Let’s understand this stepwise:

Output:

Inside the Decorator: 
Inside the Inner Function: 
'Decorated the function'
Inside the actual function

Visual Representation of the Code Execution:

Inside the Decorator Execution:

Decorators with Parameters in Python

Inside Inner Function Execution:

Decorators with Parameters in Python

Decorator Function Execution:

Decorators with Parameters in Python

Final Output Execution:

Decorators with Parameters in Python

In the above code, we will get the outputs from the functions called using decorators with parameters.

Alternate Way:

In the following code, we will see how to write a code for using decorators for functions in an alternative way.

Output:

Inside the decorator
Inside the inner function
Decorated the function
Inside the actual function

Now we will see different examples of using decorations with the parameter for better understanding the concept.

Example 1:

Output:

Inside the Decorator
Inside the inner function
I am studying COMPUTER SCIENCE AND ENGINEERING 
Inside the actual function

Visual Representation of the Code Execution:

Decorators with Parameters in Python

Final Output Execution:

Decorators with Parameters in Python

Example 2:

Output:

I am studying COMPUTER SCIENCE AND ENGINEERING 
Summation of values - 36
Computer
Science
and
Engineering

The above example also shows that the enclosed inner function can access the parameters of the outer function.

Visual Representation of the Code Execution:

Decorators with Parameters in Python

Example 3:

Output:

Decorator for 'string_Join'
Decorator for 'summation_1'

stringJoin process started ...
I am studying Computer Science and Engineering

summation process started ...
The sum is equal to: 1242

Visual Representation of the Process:

Returning decorator_1:

Decorators with Parameters in Python

Returning wrapper_1:

Decorators with Parameters in Python

Decorators with Parameters in Python

Executing message_1:

Decorators with Parameters in Python

Executing String_Join:

Decorators with Parameters in Python

Executing summation_1:

Decorators with Parameters in Python

Final Output Execution:

Decorators with Parameters in Python

Conclusion

In this tutorial, we have discussed how to execute the function using a decorator with parameters. We have also explained examples using a visual representation of inner functions and Outer functions of the parameters.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263387.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论