Partial functions in Python are SUPER NEAT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
fun tools. partial is a relatively little known part of python that allows you to create partial functions instead of lambdas or name functions that are based on other functions there are a number of cases where partial functions are more readable than the Alternatives and actually they can be quite a bit faster as well to execute while they are very simple you can do some pretty crazy things with them too if you like this video then make sure to leave a like to let me know and maybe subscribe if you want to see more videos like this be with all that out of the way let's see what these partials are all about so to actually start using partial we can do from Funk tools import partial and then I'm going to use um a doubling function to show this off but I'm going to have def malt here that takes X and we're going to keep it simple and just use integers uh and then we've going to return x uh multiply by y I'm fully aware that I can import mole from operator to do the same thing but I want to be you know a bit more explicit when I'm explaining certain things so we're defining it ourselves so we could if we wanted to create a double function that took X as an INT and returned an INT uh that returned maltt uh X2 and what this will do is it will multiply our input number by two essentially doubling it so if we were to do if name double equals main like that and then we print uh double five uh we will get as my as the terminal kind of spoiled there we get 10 because it just multiplies The Thing by two it's not a difficult concept to understand but if you didn't want to define a whole new function for that you could use partials instead so we could do a double equals partial and then we provide a a function so maltt and then we provide any number of arguments so we're going to provide two and what this will do is it will create a a partial function you can think of it as so it creates a function with two already passed to it so when we call the double function two will be set to X and then we're just passing something to Y so you can sort of think of it like if I undo this one and then reverse the logic you can sort of think of it as exactly the same as this so if we were to then run it we would see we get 10 back and this just saves you from having to create a whole new function with type signatures and everything and then you do all this when you can just do a single line thing and it actually preserves the typ pins you can see it registers as a partial that returns an integer if I to get rid of all this and do it again you could see that it recognizes that not only is it an integer but it's the parameter y so it's actually able to use malts function signature to generate its own function signature for the partially initialized function which is really cool on top of being more readable in my opinion and certainly a lot more convenient in a number of instances it's actually a little bit quicker to create a partial function is to create a brand new function and I've created this Benchmark to show that off so in our first test here we have our double fight it's exactly the same thing as before but we have a Lambda X and you know what for the sake of full uh kind of full parity I guess uh we will will do it in the same way that partial would so we create a Lambda function and then we multiply 2 by Y and in this case we are importing operator or mle from operator in this case down here for the partial we are importing partial and then we create a partial function much like we did in the other script and then we just print the results down here and then if I do that Benchmark we can see that partial is about 1.6 times faster to run I will say it is a little bit slower to create a partial function than it is to create a Lambda but executing it is actually on average 1.6 times faster at least for this particular function uh the reason for that I am really honestly not sure if anyone does know why partials are more optimized and do let me know in the comments I believe it's because it's not actually creating a whole function it's just using a function and creating a class to execute it or something um you think that would make it slower really creating a whole class rather just a function but then a function is an object I don't know if anyone knows do let me know um but it is it is faster to run it it's consistently faster to run as well so for something like this as I said 1.6 times faster if you wanted to use partials for methods instead then you could so as you can see I've set up our previous example inside a class if we import partial method from here we put partial method down here and then we indent this to be a member of the class and then I'm just going to create a quick instance uh test double I don't know why this is complaining at me uh oh yeah we need a self that would help but now if you run that uh a script we can see that it runs the same thing so we've now created a partial method within our class so those are the basics of partial method I do want to show one more thing and this is one of the crazier things you can do with a partial the partials are already very situational I will admit but you can do some wild things and one of them is an optionally parameterized decorator so what I mean by that is a decorator you can call with or without the brackets so if we go into decorator here and I'm just going to copy and paste this all in uh so this is from The Decorator video I haven't copied it at all weird uh this is from The Decorator video I did last week if you are unfamiliar with decorators I recommend you go and watch that so currently the only way that we can call this decorator is like this without the brackets if we did do the brackets then it would crash you can you can actually see it's going to error here um saying it's missing a positional argument call however if we wanted to make it so we could do that we could uh pass we could say that funk equals none by default and in here uh if Funk is none and I realize now I need to import partial then we can return a partial of timer with all our other arguments so let's say we had an argument here uh that was for Bose uh and then we did verose equals verose like that and then our verose behavior and I'm just going to copy paste this from my notes we just say you know if for both we Adit when it started and then when it ended like that uh you could still run it like this and it'll run perfectly fine you could also run it like this and it would run perfectly fine you could also run it with fose equals true and it would oh it would help if it actually printed anything why is it not printing anything oh I'm running the wrong bloody thing that's why decorator there you go uh so we have our started at and our ended at times if I just go through again so if you run it like this we can see it runs perfectly fine if you run it like this we can see that it also runs perfectly fine and that is just the power of partials now the one caveat to this is you are limited to keyword arguments if you want something that is kind of a bit more predictable you could probably do it with a function or you probably you could probably do it with arguments if you were to check if the function was a function and if it wasn't then do this otherwi otherwise pass things through but that just gets a bit messy um on on a programming Paradigm that is probably not recommended anyway um but yeah this is just one of the fun things you can do you know with a partial if you wanted to let me know in the comments what programming problems you've come across that a paral would be really useful to solve now that you know about it if you do want to learn more about decorators including how to create parameterized decorators like actual parameterized decorators and how to type them properly then I did a video All About decorators last week go and watch and I'll see you in the next one for whatever we do next
Info
Channel: Carberra
Views: 4,223
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: w67gx2JdTYg
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Mon Jun 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.