Demystifying Python Decorators: A Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone I hope you're having a wonderful day and welcome to my video on decorators in Python decorators are a really useful way of extending the behavior of a function without having to modify its underlying implementation you've probably came across decorators before if you've used a library such as flask and we can see here on line five this app. route that starts with a little at symbol is an example of a decorator to fully explain what decorators are I first need to go over what a first class function is and also what higher order functions are and then afterwards I'll explain a bit more in detail about what decorators are and then how they can be used in practice so a programming language is said to have first class functions if functions can be treated just like any other variable that is they can be assigned to variables or they can be passed as an argument to functions they can be returned from functions and so on pretty much every mainstream language now supports first class functions uh and effectively every functional programming language too higher order functions so a function is said to be higher order if it either takes one or more functions as arguments or returns a function so a higher order function is just one that manipulates functions in some way and these are kind of academic sounding terms but don't worry about it too much it's just good to know for going on so as a little example of a higher order function let's look at this I have a function here called apply and this is our higher order function it takes a function as an argument it takes an X and it takes a y and then it just Returns the application of X and Y to the function so you call FN with X and Y and it returns it then we have these non- high order functions here called add and multiply and all they do is just add their arguments and multiply their arguments and to invoke these all we have to do is say apply to the ad function the arguments 10 and five and apply to The Malt function the arguments 10 and 5 we print out the results and we get 10 and 5 is 15 and 10 * 5 is 50 which makes sense and you're probably wondering okay that doesn't really do anything useful it's a bit of a contrived example but I'll show you here an actual useful example of a higher order function so we have this high order function here called log that takes a function and returns another function and I'll explain this a bit more in depth in a second and then we have these functions from the preview example add and malt and then all we're doing here is saying add is the result of calling log with the ad function what this does it calls it Returns the wrapper and assigns that to add the same with the malt function return the rapper assign it the Mt and now whenever we invoke ad so we call it like so it starts to invoke this rapper so we get the name of the function which in this case will be ad we say calling ad with the arguments uh like 5 and 10 and then we return the result of calling ad with the arguments that were supplied and we do the same for malt here and I won't go through it again and all we do here is print the results and now it says here's the logging calling add with arguments 10 and five calling mul with the arguments 10 and five and then here's the console logs of the actual results so we've managed to add a little log to every call of these functions without having to modify how ad and Mt are actually implemented which is pretty useful to be able to do again this is a bit of a contrived example moving on so decorators are just a bit of a syntactic sugar that M creating and using these high functions a little bit nicer syntactic sugar doesn't actually add anything new to the language it just makes it easier to express certain Concepts or do certain things so let's rewrite the previous example using decorators so if we look at this log function it's the exact same function as before except now instead of doing add equal log ad we just add a little decorator that says you know wrap this ad function with the log and the same for malt we just add this little decorator above it and now whenever we call add it'll automatically wrap it in this uh wrapper function and it produces the exact same output as the example before and just as a final example here is a way that we can use decorators to time how long a function takes to execute so I have this little timer here and I'll explain how it works we're returning or we're defining an inner function called rapper we get the name of the function we get the start time and then we actually call the function and then we get the end time and the elapse time is just how long it took between the end and the start so it might be you know a few seconds a few minutes it could be anything and now we just print the result so we say the function took you know elapsed to four decimal places second and then we return the result so I have a little example of a slow function here that just goes to sleep for 5 seconds and then afterwards it prints that the slow function is finished and if you notice there's a decorator that says use the timer function when we call it it'll take 5 seconds and then after after 5 Seconds it'll print out the slow function is finished and that the function took 5.1 seconds and this little fraction of a second is probably just having to print out that the slow function is finished so there's a little intro to what decorators are how they're related to high order functions and how you can use them to implement something actually useful if you have any questions please leave them in the comments below and uh yeah thank you for joining me today have a nice one bye right
Info
Channel: Church of Turing
Views: 367
Rating: undefined out of 5
Keywords: python, decorators, programming, data science
Id: -qZ2S0NnVMA
Channel Id: undefined
Length: 6min 31sec (391 seconds)
Published: Wed May 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.