1 00:00:00,350 --> 00:00:03,060 Lambdas let us write throwaway functions. 2 00:00:03,060 --> 00:00:03,790 Sometimes, though, 3 00:00:03,790 --> 00:00:07,220 we need to be able to build up arguments to a function over time. 4 00:00:07,220 --> 00:00:10,060 Maybe you have a function that sorts a variable based on another 5 00:00:10,060 --> 00:00:12,750 function that you pass in, but you want 6 00:00:12,750 --> 00:00:16,730 to offer some quick access functions with the sorting function already set. 7 00:00:16,730 --> 00:00:20,860 This is where a fairly new part of the functools library comes in handy. 8 00:00:20,860 --> 00:00:23,320 A little function named partial. 9 00:00:23,320 --> 00:00:28,570 Partial lets us create a function with an incomplete or partial list of arguments. 10 00:00:28,570 --> 00:00:31,080 This idea of having partial arguments to a function 11 00:00:31,080 --> 00:00:35,570 also leads to another functional programming aspect known as currying. 12 00:00:35,570 --> 00:00:38,480 Currying lets us return alternate versions of our function 13 00:00:38,480 --> 00:00:41,570 from inside of the function based on the number of arguments that come in. 14 00:00:42,750 --> 00:00:44,020 Let's go see about partials. 15 00:00:45,340 --> 00:00:47,820 >> Now, I have to admit that partials are something 16 00:00:47,820 --> 00:00:50,930 I don't use a whole lot in Python and neither is currying. 17 00:00:50,930 --> 00:00:53,250 But that said, just because I don't use them all the time, 18 00:00:53,250 --> 00:00:55,790 doesn't mean they're not handy and good to know about. 19 00:00:55,790 --> 00:00:58,950 And it also doesn't mean that you won't find good uses for them. 20 00:00:58,950 --> 00:01:03,066 And I feel it's better to give you more tools to use than I necessarily use myself 21 00:01:03,066 --> 00:01:06,970 because you might find them really, really useful later on. 22 00:01:06,970 --> 00:01:08,950 So let's talk about partials. 23 00:01:08,950 --> 00:01:13,920 Partials let us partially call or partially apply a function. 24 00:01:14,950 --> 00:01:17,950 The idea is if you think about a function that takes more than one argument, right? 25 00:01:17,950 --> 00:01:20,840 We'll say a function takes two arguments. 26 00:01:20,840 --> 00:01:23,500 If we can always supply one of those arguments, 27 00:01:23,500 --> 00:01:26,970 then we only have to supply the second one whenever we want to use that function. 28 00:01:26,970 --> 00:01:29,060 I think it's easier just to illustrate this with a function. 29 00:01:29,060 --> 00:01:31,489 So let's do a function, we'll say mark-down. 30 00:01:31,489 --> 00:01:34,400 So we're gonna take a book, and we're gonna take a discount. 31 00:01:36,900 --> 00:01:38,915 And we're gonna, of course, copy the book. 32 00:01:38,915 --> 00:01:43,927 And then we're gonna say the book's price should be equal to the rounded 33 00:01:43,927 --> 00:01:49,790 version of the book's price minus the book's price times the discount. 34 00:01:49,790 --> 00:01:51,790 And we want two decimal points on that. 35 00:01:51,790 --> 00:01:53,390 And then we're gonna return the book. 36 00:01:55,020 --> 00:01:57,390 Okay, so nothing really fancy here. 37 00:01:57,390 --> 00:02:01,520 This is a lot like our sales price function from earlier, only in this case 38 00:02:01,520 --> 00:02:06,887 we're taking a customizable discount, so you can discount the book by 0.2 for 20%. 39 00:02:06,887 --> 00:02:10,800 You can mark it down by 0.5, for half price, stuff like that. 40 00:02:10,800 --> 00:02:13,220 In fact, let's do that. 41 00:02:13,220 --> 00:02:19,940 Let's say our standard mark down is partial mark down. 42 00:02:19,940 --> 00:02:23,640 Mark down is the function that we want to call, so partial, 43 00:02:23,640 --> 00:02:27,440 we're gonna import this here in a second, partial lets us partially apply the thing. 44 00:02:27,440 --> 00:02:31,810 And then this is the thing we want to partially apply, mark down. 45 00:02:31,810 --> 00:02:35,560 And the argument that we want to set is we wanna set discount and 46 00:02:35,560 --> 00:02:37,480 we wanna set it to 0.2. 47 00:02:37,480 --> 00:02:38,180 Okay. 48 00:02:38,180 --> 00:02:43,850 So let's come up here and we need to import partial from func tools. 49 00:02:45,770 --> 00:02:47,290 Okay. So the standard partial. 50 00:02:47,290 --> 00:02:56,135 So now let's do print(standard(BOOKS[0]).price). 51 00:02:56,135 --> 00:03:02,137 That should show us the markdown 52 00:03:02,137 --> 00:03:05,920 price of that book. 53 00:03:07,710 --> 00:03:08,360 Yeah, we get 10.84. 54 00:03:08,360 --> 00:03:14,250 And if we were to do books, say 5, we get 6.39. 55 00:03:14,250 --> 00:03:16,290 Okay, so there's our markdown price. 56 00:03:16,290 --> 00:03:17,480 So that's cool. 57 00:03:17,480 --> 00:03:18,830 So let's do another one here. 58 00:03:18,830 --> 00:03:19,960 Let's do a half price. 59 00:03:19,960 --> 00:03:26,432 So half = partial(mark_down, discount=.5) so 60 00:03:26,432 --> 00:03:33,460 if we change this to half instead of standard now we get $4. 61 00:03:35,080 --> 00:03:38,160 So that's all great, that's all well and good, but 62 00:03:38,160 --> 00:03:40,080 here's where this comes in really handy. 63 00:03:41,350 --> 00:03:49,390 So let's say that we want to mark down to half price all the books. 64 00:03:50,600 --> 00:03:55,080 So we would map half to BOOKS. 65 00:03:55,080 --> 00:04:01,050 Or maybe we wanna do it only to our long books, right? 66 00:04:04,270 --> 00:04:06,060 That was our function earlier, right? 67 00:04:06,060 --> 00:04:08,250 is_long_book Is long book, yeah. 68 00:04:09,770 --> 00:04:11,790 All right so our long books, we're gonna make half price. 69 00:04:13,140 --> 00:04:19,149 So then let's just print a list of half price books. 70 00:04:22,358 --> 00:04:23,080 And there we go. 71 00:04:23,080 --> 00:04:25,380 These books here, whatever these books are, 72 00:04:25,380 --> 00:04:30,190 these are the long books, and we're gonna mark them off half price. 73 00:04:30,190 --> 00:04:33,380 We're gonna mark them to half of their normal price. 74 00:04:33,380 --> 00:04:34,450 So that's pretty cool! 75 00:04:34,450 --> 00:04:38,710 We were able to combine a map and filter and our partial. 76 00:04:38,710 --> 00:04:42,293 So we can write all of these partials for all of our common discounts, and 77 00:04:42,293 --> 00:04:46,228 then we're just like, oh yeah, let's apply 50% off to these ten books, or 78 00:04:46,228 --> 00:04:49,030 75% off to these books or whatever. 79 00:04:49,030 --> 00:04:49,890 So that's where partials are. 80 00:04:49,890 --> 00:04:52,460 Partials are handy for that kind of stuff.