Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Sometimes, an entire function, even an anonymous one, is just too much. Partial functions let us partially apply a function. Partials aren't as common as the other things we've covered, but they're still a handy tool.
functools.partial
lets you preset some arguments to a function. You can then call the new function with the remaining arguments as needed. This often ends up being really handy when used with map()
and filter()
.
Lambdas let us write throwaway functions.
0:00
Sometimes, though,
0:03
we need to be able to build up
arguments to a function over time.
0:03
Maybe you have a function that
sorts a variable based on another
0:07
function that you pass in, but you want
0:10
to offer some quick access functions
with the sorting function already set.
0:12
This is where a fairly new part of
the functools library comes in handy.
0:16
A little function named partial.
0:20
Partial lets us create a function with an
incomplete or partial list of arguments.
0:23
This idea of having partial
arguments to a function
0:28
also leads to another functional
programming aspect known as currying.
0:31
Currying lets us return alternate
versions of our function
0:35
from inside of the function based on
the number of arguments that come in.
0:38
Let's go see about partials.
0:42
>> Now, I have to admit
that partials are something
0:45
I don't use a whole lot in Python and
neither is currying.
0:47
But that said, just because I
don't use them all the time,
0:50
doesn't mean they're not handy and
good to know about.
0:53
And it also doesn't mean that you
won't find good uses for them.
0:55
And I feel it's better to give you more
tools to use than I necessarily use myself
0:58
because you might find them really,
really useful later on.
1:03
So let's talk about partials.
1:06
Partials let us partially call or
partially apply a function.
1:08
The idea is if you think about a function
that takes more than one argument, right?
1:14
We'll say a function takes two arguments.
1:17
If we can always supply
one of those arguments,
1:20
then we only have to supply the second one
whenever we want to use that function.
1:23
I think it's easier just to
illustrate this with a function.
1:26
So let's do a function,
we'll say mark-down.
1:29
So we're gonna take a book, and
we're gonna take a discount.
1:31
And we're gonna, of course, copy the book.
1:36
And then we're gonna say the book's
price should be equal to the rounded
1:38
version of the book's price minus
the book's price times the discount.
1:43
And we want two decimal points on that.
1:49
And then we're gonna return the book.
1:51
Okay, so nothing really fancy here.
1:55
This is a lot like our sales price
function from earlier, only in this case
1:57
we're taking a customizable discount, so
you can discount the book by 0.2 for 20%.
2:01
You can mark it down by 0.5, for
half price, stuff like that.
2:06
In fact, let's do that.
2:10
Let's say our standard mark
down is partial mark down.
2:13
Mark down is the function that
we want to call, so partial,
2:19
we're gonna import this here in a second,
partial lets us partially apply the thing.
2:23
And then this is the thing we want
to partially apply, mark down.
2:27
And the argument that we want to
set is we wanna set discount and
2:31
we wanna set it to 0.2.
2:35
Okay.
2:37
So let's come up here and
we need to import partial from func tools.
2:38
Okay.
So the standard partial.
2:45
So now let's do
print(standard(BOOKS[0]).price).
2:47
That should show us the markdown
2:56
price of that book.
3:02
Yeah, we get 10.84.
3:07
And if we were to do books,
say 5, we get 6.39.
3:08
Okay, so there's our markdown price.
3:14
So that's cool.
3:16
So let's do another one here.
3:17
Let's do a half price.
3:18
So half = partial(mark_down,
discount=.5) so
3:19
if we change this to half instead
of standard now we get $4.
3:26
So that's all great,
that's all well and good, but
3:35
here's where this comes in really handy.
3:38
So let's say that we want to mark
down to half price all the books.
3:41
So we would map half to BOOKS.
3:50
Or maybe we wanna do it only
to our long books, right?
3:55
That was our function earlier, right?
4:04
is_long_book Is long book, yeah.
4:06
All right so our long books,
we're gonna make half price.
4:09
So then let's just print
a list of half price books.
4:13
And there we go.
4:22
These books here,
whatever these books are,
4:23
these are the long books, and
we're gonna mark them off half price.
4:25
We're gonna mark them to
half of their normal price.
4:30
So that's pretty cool!
4:33
We were able to combine a map and
filter and our partial.
4:34
So we can write all of these partials for
all of our common discounts, and
4:38
then we're just like, oh yeah,
let's apply 50% off to these ten books, or
4:42
75% off to these books or whatever.
4:46
So that's where partials are.
4:49
Partials are handy for that kind of stuff.
4:49
You need to sign up for Treehouse in order to download course files.
Sign up