Functools is one of the MOST USEFUL Python modules

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
at this point throughout the pythony's awesome series I've covered a lot of really cool and really useful python libraries like editors and Collections and all that stuff but I haven't yet covered Funk tools and there is a good chance that you've seen some functionals usage around and Funk tools is actually one of the more useful um libraries to have especially if you're thinking about doing some really junk stuff it's quite powerful in terms of that so if I just bring my notes to the other screen uh there are three things I'm going to talk about as always I have kind of picked out certain things that I want to talk about you know the most useful things I can think of in this case I've chosen the things you've probably seen around um so there is actually a really good chance that you've seen this already and maybe you know don't know what it does um but there is some other stuff to it funk tools isn't as extensive as other um packages so there is only really one or two other things the reason I'm not necessarily including those is because they're either you know alternative implementations of stuff or really complicated probably you know to the point where it could have its own video so I'm going to be covering wraps partials and caching today so we're gonna do from Funk tools uh import wraps partial and then cash the reason I'm doing like that that is because generally speaking from what I can tell um that is kind of the convention to do you know from Funk tools import wraps from Funk tools input partial and just do that so I was wondering like this and we're going to be predominantly working on a function called ads which simply as the doc string says as two numbers together and the doc string is important uh for something we're going to be showing you and that just returns and actually no it's going to print X Plus y just just saves us over to do a load of prints later down the line so the first thing I'm going to show you is about the wraps so the wraps um is useful when using decorators and it basically kind of adapts The Decorator to have the correct information when you choose to present it that was kind of a bad explanation but if I oh yeah I'll show you what I'm talking about and then it'll make more sense so we have our width greeting which takes our function and then we have our deaf wrapper which takes our args and in quags and this is how you do it normally without Funk tools.waps and then we're just going to have uh print hello world and then we're going to return the funk with our args and then our quarks and then we're going to turn wrapper so that is a standard decorator I've already done a video on decorators if you're not familiar with them and then we're going to decorate our function uh with our greeting decorator and then we're going to do is we're going to do and I'm gonna do it like this because I'm fancy I've also done a video on what that means if you want to see that um and we'll do you know add two and then five and then we'll print add dot name so this prints the function name and then print add dot doc which prints the function doc string and if I run a func tools tut.play we can see that we get the hello world that is printed here we um you know calculate our sum to be seven and then the name is wrapper and the dock is none which shouldn't happen or at least we don't want it to happen so this happens because the function well this happens because of some weirdness um so when the decorator you know decorates a function it returns this wrapper which is executed kind of instead of this so this gets passed into here and then what's actually being executed is this and when we return the wrapper we're technically transforming this function into this function um which one doesn't have a doc string and has the name wrapper so what we can do is we can decorate the wrapper with wraps and then provide the the actual function object and what this does is it changes the name of this function to this function and then adds the doc string to it so if we run it again we can see we have the same hello world we have the same seven but our font name is now add which is you know correlative to this and our doc string is adds two numbers together which is correlative to this so generally speaking you pretty much always want to use Funk tools wraps which is why out of all the things in this video you've probably seen that one the most so if you ever unsure about what that did it's simply just a mechanism it's it's kind of an optional mechanism you don't really need it but it's just a mechanism to make you know using decorators a little bit nicer and a little bit you know you know kind of reduce unexpected Behavior which is kind of cool so I'm just going to reset everything because we're going to continue using the same function I will keep I'll keep the decorator there um but we'll just remove it from this and we're going to talk about partials next partials are defined in the python documentation as freezing kind of certain implementations to then be run later and I prefer to think of it more like we're pre-loading functions ready to be run so a partial is essentially a function with an argument already pre-passed to it so we can have our ad here or we can have you know uh ads two and five uh equals partial add to nn5 so what this does is it creates a partial object where the funk is uh add so this is the thing that's going to be called and two and five are our arguments so 2 is going to be passed to X and 5 is going to be passed to Y and then if we do add 2 and 5 and we don't pass any arguments at all we get seven once again because we have pre-loaded two and five into our arguments and this is particularly useful if you have like a for Loop or something that's setting um functions to lay variables or something because if you do it with a for Loop uh sometimes depending on the actual implementation the arguments will always be set to like the last ones it's it's kind of strange and weird to get your head around but in a sense well in a sense the Frozen thing kind of comes into it because this is now set you know if we had you know x equals three here and then we set this to be X and then we set x equals four this would still be eight because we've now you know set X because X was three when the partial was created this is now set as three so it doesn't matter what we do with X later on X will always be three or when we call add two and five we'll always be passing 3 to X um so I'm going to set that back um and you can actually do this with just a single argument so doing something like this is perfectly fine um and you can do add two and then you have say five and then we get our seven and seven so what this is doing is it's preloading two as X but y still hasn't been set so we can then pass Five in here so this five is going to the partial object which already has something for X so this five is actually y and then we add two to it as we do in the function because X is set to 2 in the partial hopefully that makes sense uh it's probably one of those things where you you might need to kind of use it to get your head around it um but partials are really really useful uh there are some situations where you would actually want to use partials over Landers so lambdas don't have this freezing property about them so if you need something to be Frozen like you had in that X example earlier then you'd want to use partial and not Lambda for that so I was just about to move on to the next thing and I actually forgot to mention something uh you can have uh you can use partial for methods as well but you do need to use the separate partial method um function rather than partial if you're doing that they work slightly differently but it basically works for methods so there is a separate method implementation um in case you want to do that Source thing with methods instead of functions uh so the next thing we're going to talk about or the final thing we're going to talk about today is the cash so if I stick this here this is now a cachable function so basically what this does is it caches the return value of a function um based on its parameters so I will actually need to to do a return X Plus y here and then we will need to do print two and five and to show this off I'm going to import time and then we're going to simulate a very complex adding function by doing time dot sleep two so this is going to sleep for two seconds uh this simulates a particularly expensive callable which is kind of the only reason why you'd want to do it with a cache because obviously if you cache something that increases the memory complexity ever so slightly but of course it does reduce the time complexity so for then prints add two and five and then we print add two and five again I am going to do a print running so you can kind of see you can see that it's running it's waiting for the two seconds and then we get the seven twice uh once when we're calling it from here and once when we're calling it from here you know if I do a not cached uh this should make it a bit clearer cache like that don't cover on Earth hold on what have I done there we go I think I just hit backspace by accident so you can see they're not cash result is seven and the cash result because it's the same parameters are seven but if we were to run this again with say three and five it would need to actually run again because they're not the same parameters so this caching does allow you to pass different things into the function but caches the same things you know if you're going to be calling the same function with the same parameters over and over again and the caching is something you probably want to look into one caveat is that the order of operations does matter so if I were to say do 5 and then two instead of two and five it would run the first time it would then run the second time and then run the third time because while the numbers are the same X and Y are different values and the cash is no way of knowing that you're passing the same numbers in it just checks against the actual parameter values so just keep in mind that ordering does matter even if ordering doesn't matter in your implementation you might want to change it so it does if you are going to use the cache especially if it's something particularly expensive there is also if you have like a property which is actually how I learned about this existing in the first place you have your cash property here as well um which essentially Works kind of the same just uh you know the property only needs to run once so you only want to do this if the property is probably immutable but particularly expensive to calculate um for whatever reason so you then have this property so whenever you access it again it then accesses it to the cache rather than having to do all the expensive calculations again um you can also do well it's it's essentially the equivalent of using the property and then the cash decorator separately which you can do but you also have cash property if you just want to use the one decorator for it um but yeah that's everything I wanted to talk about with func tools there are some other cool things as part of funk tools but as I said they're either just slightly different ways of doing things or you know so complicated that they need their own videos which I'm thinking about doing but uh I don't necessarily know um and if I do a video on this topic you'll understand why I'm thinking about maybe not doing it but yeah if you like this video then make sure to leave a like to let me know and maybe subscribe so you don't miss out on future videos like this if you have any questions or ideas for future videos then feel free to leave a comment I read them all um so the feedback would be greatly appreciated if you want to support this channel monetarily you can do so on one of two ways first of which by becoming a patron the second of which by becoming a member using a join button below uh one pound a month on either and you can build this screen like these people and I will see you in the next video where I may come back to Funk tools I may decide to do something else it's probably going to be something else and then I might come back to it the following week um but uh yeah I'll see for whatever it may be next Saturday
Info
Channel: Carberra
Views: 32,048
Rating: undefined out of 5
Keywords: pyfhon, pytho, pytbon, pytjon, ptyhon, pytyon, ptthon, pyyhon, pythn, pythoh, pythpn, ython, pytgon, pyhon, pytohn, phthon, oython, pthon, pyghon, pythoj, pythno, pythkn, ypthon, pytuon, lython, pyrhon, pythom, pythob, puthon, pgthon, python, pyhton, pythln, pythin, pytnon, pyton
Id: B9yM53qkbW0
Channel Id: undefined
Length: 13min 36sec (816 seconds)
Published: Sat Jan 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.