What is functools.wraps in Python 🐍

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let me tell you what fun tools reps is about so if you go to the module function tools you can import reps and this is a decorator so reps is a decorator and it's very useful for when you're defining your own decorators so let me show you something let me Define a decorator that is going to scream before the function is called so this screen decorator this is going to go and wrap a function that's going to take some arguments that I do not know beforehand because I don't know where my decorate will be applied and the only thing I'm going to do is scream and then call the original function with all of the arguments and the keyword arguments and this is returning my wrapper on the function and there you have it now I have my decorator and let me apply it to the function f that takes two numbers and adds two numbers this is the doc string for my function and it returns a plus b now if I add 3 with 4 I get 7 but first I get a screen now what's interesting is that if I type in help f I get this it says help on function wrapper in module 9 and it says wrapper star arcs star star quarks and if I just type in F it says that this is the function screen.locals.wrapper instead of the function f now the issue is The Decorator scream returned wrapper and rapper did not preserve the useful information that I defined in my function and this is where raps comes in I use reps to say that some function of mine will be wrapping another function and that I need to copy this meta information around so let me redefine my screen decorator and before defining the wrapper function I used the wraps the Creator to say that whatever comes after will be wrapping the function f and I can Define my reference before and so this is the star star quarks first thing I do is Scream a lot and then I can return the result of calling the original function with the original arguments and now I can return the wrapper and now I can apply this decorator to the same function that's going to have so this adds two numbers this returns a plus b and now I can call the function the function still works and it screams before returning the result and now the function looks exactly like he choose look so the f is the function add and I can check the help on ADD and it's going to say that have AV adds two numbers so that's what the funk tools wraps is for it's to preserve metadata when you define your own decorators
Info
Channel: Rodrigo 🐍🚀
Views: 3,992
Rating: undefined out of 5
Keywords: python, python3, tutorial, programming, computer programming
Id: pS38GFwhPTY
Channel Id: undefined
Length: 3min 6sec (186 seconds)
Published: Sat Sep 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.