Why use partials

If we define a function in Python, we can easily set default values for the parameters. What is the difference between doing this and using a partial? More importantly what are the practical use cases when we should use partials?

In the screenshot below I cannot set a default value for just person, I need to set one for rank as well

however with partials we can do that

Is that the only difference between the two?

Thanks

2 Likes

You should use partials when you’re reusing code and adjusting some parameters often (imo). An example case is our optimizer functions, where we want to pass in custom parameters (but not all the parameters)

2 Likes

Adding to what @muellerzr said, on a more general note, you can use partials anytime you want to set default values for any functions you’re using from libraries, like maybe setting some flags in opencv functions.
Also, if you’re passing some library function as a callback to another module but you do not have direct control over how it is going to be invoked (for example, callbacks for FastAI’s Learner where your custom parameters won’t be passed), that is one place where you can use Partials.

2 Likes