Use THIS To Make Your Code MUCH MORE Reusable In Python (Partials)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you how you can simplify your functions or actually freeze them in certain ways so that you can reuse them with a simplified signature and this is going to be very similar to currying so I'm going to show you both what it means to carry something and also the alternative which is called partial and it is very similar to carrying so I wanted to show you both Alternatives but this is going to simplify your functions a lot so that you can reuse them much easier but before we get into the main purpose of this video I'm going to show you some currying so what is currying and the best way to demonstrate this is to create a very simple function that's called multiply setup and that's going to take a number of type floats now inside this function we're going to create another function a nested function called multiply and here we're going to type in B of type float and this will return to us a float now the tested function is going to return a times B and the outer function is just going to return the inner function so this is the first example of currying because now what we can actually do is something like this we can say print multiply setup and here we can insert two so we want to multiply everything by two and then on the outside we're going to multiply the number of three and if we run this we will get 6 in the console so as you can see we can use a parenthesis immediately after another parentheses because we're returning the inner function but this is not really a common use case a common use case would be to create another function for example double and say that the multiply setup for double is going to be 2 and we can do something similar under which is called triple and that's going to take multiply setup and it's going to take three so with both of these being done we can now print the double of 10 and we can also print the triple of 10. so we've done the complex setup here now all we have to do is reuse that function with that setup so we run the function now you'll see that double effectively doubles the number and triple triples the number and you can go even deeper than that if you want you can add another nested function and another but that's the basic concept of currying and the downside to this is that it can get really complicated really fast in your logic when you're defying a function that is being curried so a very nice alternative is the partial function from Funk tools so immediately to use that we need to import from func tools be partial now we're going to try to imitate what we did earlier but this time I'm going to add some added functionality so here we'll type in Def multiply and it's going to take an integer or a float called a and a float called B and it's also going to take a name of type string or none so it's an optional and finally it's going to return to us a float now if there is a name we're going to print this verbose text so it's going to say formatted and we're going to add a new line which is going to give us the name followed by a and b so we can see what we are inserting so a is going to be a and b is going to be B and of course all we want to do is return a times B so it's a very simple multiply function just as we did earlier all it does is multiply two numbers and return the result so let's do what we did earlier except this time we're going to use partials so here we can type in double and double is going to equal a partial and if you look at the parameters it accepts first it takes the function name so multiply without the parentheses because we're going to specify the arguments and the keyword arguments immediately after so now for the argument I'm going to insert the number two and we can duplicate this and say triple and he will insert the number three so already you can see this syntax is a lot easier to understand than what we were doing when we were occurring now we can print double of 10 and we can print the triple of 10 and we're going to get the same result or actually we're missing a required argument and that is because this optional should actually be equal to none initially but if we run this now we'll get a 20 and 30. and if name should also be checking that it is not none because I forgot that an empty string also evaluates two false but going back to the partials we've created the same thing that we did with curing except this time we used a partial and using a partial does have some more advantages because right now it helped us create a function that was easy to use and it helped us to specify the parameters we wanted to include and I didn't really show you but you can also add keyword arguments such as name and you can say this is double and when you run this it's going to give us the verbose version so double with a being 2 and B being 10. so we can add the keyword arguments to these and you can also specify which numbers you want to use in which order so if you type in B is equal to 3 and here we'll type in triple and this actually has to be name is equal to Triple you'll see that the 10 we're inserting is going to be passed into a instead of being passed into B so if we run this you'll see now that we have double that takes a as 2 and B is 10 because here we covered the first argument with two which means that the second argument was going to be 10 but here we specified that b is going to be equal to 3 so the only missing argument we have is a so now when we pass in a it passed it into the first slot which is a so that's something more complex you can do with partials and a major difference with curing and I'm just going to bring it back because it's easier to compare it side by side so a major difference with curing is that you can only use one parameter at a time or one argument at a time so first you need to specify a then you need to specify B so if you have plenty of arguments and keyword arguments you cannot do that incurring it just takes one at a time so you can slowly simplify that but with the partial function you can specify as many arguments as you want inside that partial to simplify it so you can create that setup to make this as easy to use as possible so maybe for a multiply function it's not that useful but sometimes you'll have these functions with a lot of parameters and a lot of arguments and at the end of the day you might want to have something such as Let's Pretend This is a get URL so we'll change that to get URL and at the end of the day you might want to have something that just takes a URL so www.subscribe.com for example and this is the final result of what we want to use a simplified version of the function but of course all the setup will be inside here all the headers all the parameters and everything will be covered because inside that partial we took care of that setup so every time we want to reuse that we can just insert a URL and the rest of our code is going to look really clean but let's take this back because multiplying a get URL doesn't make much sense in my head so at the end of the day this is just a convenience for your code you might have a function that needs to be simplified and this is a perfectly acceptable way to do it and I would definitely recommend you play around with this because it can be something beautiful for your code if you have very complex functions that require you to repeat a lot of specific Parts but anyways that's actually all I wanted to cover in today's lesson do let me know what you think about partials or what you think about curing and which one you prefer or if you find that you use one more than the other and if there are some use cases you use one or the other in I would be very happy to hear about that but otherwise with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 32,994
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: Ldff4G5Y3fs
Channel Id: undefined
Length: 8min 23sec (503 seconds)
Published: Thu May 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.