Python Decorators - Part lll
Python Decorators - Part lll

Python Decorators - Part lll

This article is in continuation to my previous two articles on decorators. I would encourage you to read both of those articles before moving on.

  1. Python decorators - Part l
  2. Python decorators - Part ll

The main topic covered in this article is Parameterized Decorators. So, let's begin.

Parameterized Decorators

Before we even talk about parameterized decorators, let us understand why we even need parameterized decorators. To answer this let us revisit the @lru_cache decorator that we know from the previous article again in the below points but from the perspective of a parameterized decorator.

  1. It was a parameterized decorator. In short, lru_cache decorator optimizes a function that gets called repeatedly and produces deterministic output for the same input.
  2. This decorator saves the output of the function in a map, so that function is not executed for the same input again and again.
  3. @lru_cache decorator took maxsize as one of the parameters. This parameter makes the decorator restrict the growth of the cache size to the value passed as maxsize.

I hope now, you understand that decorators, in addition to changing the behavior of a function also sometimes require control parameters to offer a better environment to augment or change the behavior of a function.

But how to make a decorator accept arguments?

As we know that decorators take the decorated function as an argument and return a closure or a function that replaces the decorated function.

We need to wrap the decorator in another function, which we call the decorator factory. This decorator factory receives those arguments. Let's see the example below:

No alt text provided for this image
Example: #1

  1. decorator_factory, which wraps your actual decorator function accepts the parameters.
  2. Calling the decorator factory returns the actual decorator, which will then be used to replace your decorated function with another function.

Output:
Running the decorator factory
Running the decorator function
Running the inner function or the Closure! True
Running the actual decorated function!!        

You don't need to invoke the decorator_factory and decorator as shown in the above example, you could simply use what we have been talking about parameterized decorator till now, shown below:

No alt text provided for this image
Example: #2

You just used your first parameterized decorator.

Both the examples above will output the same data:

Output:
Running the decorator factory
Running the decorator function
Running the inner function or the Closure! True
Running the actual decorated function!!        

Class-based decorator

We can represent the above function-based decorator to the class-based decorators too as shown below:

No alt text provided for this image
Example: #3
Output:
Running --> factor
Running --> decorator
Function received : myfunc
Running --> inner: Truey        

  1. Calling the decorator_factory(True) returns the instance of decorator_factory class. The instance has an initialized self.activate attribute
  2. Calling the instance, executes the __call__() method which takes the decorated function.
  3. __call__() returns the inner() function which replaces the decorated function myfunc.

And this marks the end of our decorator series. I hope you enjoyed and understood these articles. If you liked it, please follow me on Linkedin. See you at the next one.

Aman Bhardwaj

Senior Software Engineer at Query | CyberSecurity | I love building Software Stuff

2 年
回复

要查看或添加评论,请登录

Aman Bhardwaj的更多文章

  • Creating Python Packages

    Creating Python Packages

    Introduction Once we have written our code in a Python module, it should be available to use. Now, the next step is to…

  • Python Modules - 1: The Basics

    Python Modules - 1: The Basics

    Welcome to my yet another LinkedIn article related to Python. We will try to understand what are Python modules.

  • A refresher on Compilers

    A refresher on Compilers

    Introduction For sure, compilers are one of the most complicated programs ever written. We have been writing code in…

  • Python Decorators - Part ll

    Python Decorators - Part ll

    ..

    2 条评论
  • Python Decorators - Part I

    Python Decorators - Part I

    Introduction Decorators are one of the widely used concepts in Python. I have not encountered a Python codebase, yet…

    1 条评论
  • Variable Scoping and Closures in Python

    Variable Scoping and Closures in Python

    Introduction Variable scoping works almost the same way in most programming languages with small differences. Though it…

  • Python's property() class

    Python's property() class

    As we know that in languages like Java, C++, classes are supposed to contain private data attributes as a best coding…

社区洞察

其他会员也浏览了