decorator in python | #python #coding #programming

Akansha Yadav shows how decorators wrap functions to add logging, timing, or validation without changing the original code. She builds a mydecorator function with an inner wrapper, then applies it to a sayhello function to print messages before and after the call.
Chapters
Hello everyone, welcome back to my YouTube channel. In today's video, uh, we are going to discuss about the decorators in Python. Decorators are basically the concept which will help you to modify the behavior of a function. It basically enhance the behavior of a function that you have created without changing any actual code. It basically does what? It just wraps a function with additional functionality. You can do multiple tasks with the decorators like logging, validation, timing, catching code in instrumentation, and many more things are there. There is basic syntax that you need to use is you have to write @date and then following up with the function name. Whichever function you wanted to enhance, you just have to write down the function name after writing @date in the starting. @date is going to be the prefixing in that function. Let's write down the code. So here I am defining a function called my_decorator, and inside my_decorator function, I am going to use a nested function called wrapper. So let's define my decorator function, which in which I am going to write a function which is going to be a wrapper function. And inside this wrapper function, I will be printing few things. First, I am printing something is happening before the function is called. This is just to notify us that we have not called our function yet, but something is happening. Then following up with the next line, I am calling my original function which is going to be a func Then I am printing another statement says something is happening after the function is called. So here my original function is func and following up with the next print statement just to check if I am calling my function inside the wrapper, what happens. Then I am going to return wrapper out of my decorator function. Then I am writing a code that defines a function called say_hello which simply prints hello for us. Before that, I am writing a decorator. @date is a syntax and my decorator is my function name. Then I am defining a function, say say_hello. Then I am printing hello out of it. So in this, what you are doing is we are writing a decorator before say_hello function, which means we are trying to decorate our say_hello function with my decorator function. Note this line that this is for decorating say_hello function. Then I am calling my say_hello function over here. Now I am going to run the code. We will get 2 outputs. One is going to be the print statement, which is the normally print statement that we have written. Then we will get hello. Then we will get something is happening outside the function. So basically, when we are passing a func inside the my decorator function, it takes up the next decorator function. So here you can see we got 3 outputs. Something is happening before the function is called. Then I am getting hello. Then getting again, the something is happening after the function is called. So if you like the video, don't forget to subscribe the channel and hit the like button.










