Python Decorators: Simple Example!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys so today we're going to see how decorates works okay and for this you're going to create a simple example here but basically decorates it's really um good um approach for to solve a lot of problems and improve your code okay so let's go to the code so basically we're going to create a a simple decorator def decorator okay so then usually the create to receive as a parameter function then you're going to get a function here then you're going to create the structure of The Decorator rer P the uh arguments so args keyword ARS okay then so you usually what happens like this we have a result reive our function then you pass the parameters the arcs okay then you're going to return uh the ropper okay and so let me explain what is going on here basically so we have two parts here we have this part here that let me write this print uh something something is happening before the function is colored uh let me write this here too copy paste oh copy and paste something's H happening after okay we have two parts we have the first part that represent uh what everything is going to happen before your real function that you pass by paramet execute and then you have something after this it's really helpful for to Sol a lot of let's say problems that you can encounter in your life as a soft engineer so let's suppose that before you execute a a a function that is going to validate something in the database so you want that every time that function it is be executed you want to something happening before like you want to example uh login that operation you know and L want to say when to lock that operation the history and then after you want to log again to see if it gets a or not or something happening right so okay so we create this decorator here then let's use it to do this you have to Let's create a function let's be call relo let's add The Decorator here decorator and let's print a simple message as like hello my friend hello my friend hello my friend okay um seems that everything it's good what after you execute this code here what you should expect are as a result you should expect like something like this um this message here something is happening before the function called and then uh you should expect the hello my friend message show up in the terminal and as soon this finish you're going to get this metion here something happen after the function is called okay so let's call our function here hello and let's run the code on the Creator okay okay so now you can hear here the results something happening before the function call it hello my friend something's happening after the function col it okay so one thing that you can improve is because usually The Decorator you you want to use uh it in other mths right all the functions so the reason you creating this decoration is to give us the power to be uh not rewrite this same code over and over for the same um methods that you can use this ones you know so basically what I say is like this you don't need to write this here copy and paste and do this here exactly my function like print I'm back here so okay I do this I'm going to get the same result that I use my decorator right so but imagine that I create one more function here hello to and let's suppose that I want to use this again the same logic what I supposed to do I supposed to come here cop and paste of course that I'm going to change here what my function is doing hello to but this code here it is the same the one that uh it's produced here you know in the and the hello um function and so this is quite not good because you are just like uh write and write the same code in different like maybe fires class and this is not a good uh prod of code okay so that's the reason decorate it's really good for you to use to make your code better okay um I think it it's this for today okay and next time you're going to see other nice things that is going to uh help you to improve your code and different errors
Info
Channel: Junior Stone
Views: 37
Rating: undefined out of 5
Keywords:
Id: 8Q6fus2tpLw
Channel Id: undefined
Length: 6min 40sec (400 seconds)
Published: Sun Jun 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.