Decorators in python allow you to dynamically change the functionality of another function, without altering it’s code. Show
What? Is that possible? Yes. This covers: What is a decorator in Python?Decorator is a function that takes another function as an argument, adds some additional functionality, thereby enhancing it and then returns an enhanced function. All of this happens without altering the source code of the original function. Let’s see it in action. Let’s suppose, you have a function that computes the hypotenuse of a triangle.
Output: #> 2.24 Example use case: Let’s just say, you happen to have many such functions defined in your python code, getting executed in a elaborate way. To keep a track, you want to print out what function is getting executed before actually running it, so you can monitor the flow of logic in your python code. Here, at the same time, you don’t want to change the actual content of So what do we do? Create a decorator of course.
Get Free Complete Python CourseFacing the same situation like everyone else? Build your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable. Get Free Complete Python CourseBuild your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable.
Note,
#> I am going to execute: hypotenuse #> 2.24 Nice. It displayed the custom message showing the name of the function before executing Notice, the content of The great news is: it can decorate any function and not just So,
if you want to do the same for, say a func to calculate
Nice. Easier way to decorate functionsBut, is there an easier way? Yes. Simply add
#> I am going to execute: hypotenuse2 #> 2.24 Basically what you are doing here is, decorate
#> I am going to execute: hypotenuse2 #> 2.24 Both approaches are really the same. In
fact, adding the How to create Class Decorators?While decorator functions are common in practice. Decorators can also be created as classes, bringing in more structure to it. Let’s create one for the same logic but using class.
To make this work, you need to make sure:
Output: #> I am going to execute: hypotenuse3 #> 2.24 Problem with Decorators: The docstring help is gone?!When you decorate a function, the docstring of the original decorated function becomes inaccessible. why? Because the decorator takes in and returns an enhanced but a different function. Remember?
Help on function hypotenuse2 in module main: hypotenuse2(a, b) Now, let’s decorate and try again.
#> Help on function wrapper_func in module main: #> wrapper_func(*args, **kwargs) The help does not show the docstring :(. So how to deal with this? The Solution It’s because of this reason, everytime when someone writes a decorator, they always wrap the wrapping function with another decorator called It simply updates the wrapper function with the docstring of the original function. It’s quite easy to use:
Try decorating now, the docstring should show.
Practice Problems:Create a decorator to log start time, end time and the total time taken by the function to run. Can a Python decorator take argument?Implementing Decorator Arguments
You may expect that decorator arguments are somehow passed into the function along with this f argument, but sadly Python always passes the decorated function as a single argument to the decorator function.
Which of the following is true about decorators in Python?Decorators can be chained A Decorator function is used only to format the output of another function dec keyword is used for decorating a function Decorators always return None” Code Answer.
What do decorators do in Python?A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.
How do you pass arguments in decorator functions in Python?To fix this, you need to change the repeat decorator so that it accepts an argument that specifies the number of times a function should execute like this: @repeat(5) def say(message): ... To define the repeat decorator, the repeat(5) should return the original decorator. The new repeat function returns a decorator.
|