Decorators with Parameters:

In this blog, we’ll be learning about the decorator functions in more detail. Python functions basically are objects as they are first-class citizens in python while using it for data science.

  • We can assign functions to a variable i.e.; they can be use as a refference.
  • We can pass functions as an argument to another function.
  • We can return functions from another function.

Decorators having parameters are similar to normal decorators.

The code written above is similar to,

python for data science code

Execution starts from left to right. As this happens, decorator(params) is run, and this returns a function object fun_obj. By using the fun_obj we call the fun_obj(fun_name). These  functions are run inside the inner function. The actual function reference is return, which is later assign to func_name. To call the function with the decorator , we use func_name.

Let us know how the code is seen.

Implementing decorator with parameter:

We should note that params can be empty in this case.

From the example above, we can conclude that outer function parameters can be access by the enclose inner function.

Inside the decorator:

Inside the function:

decorators in python for data science

To learn more about decorators in python for data science then you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

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