Usage of the python functools.partial method


Been surfing through this file:

and found a couple of statements encountering the usage of the “partial” python API. They are as follows:

learner.reg_fn = partial(seq2seq_reg, alpha=2, beta=1)

Now, as if the above wasn’t confounding enough, there’s a partial used for a class as well !

opt_fn = partial(optim.Adam, betas=(0.7, 0.99))

Just wondering why it is that the “partial” API was explicitly used. I used the plain old method definition, i.e.

learner.reg_fn = seq2seq_req()

and get the code to compile and train just fine, albeit the fact that the alpha and beta values are zeros. But setting the desired alpha and beta values could surely be just a little bit of code modification.

Anyways, the crux of the question: other than provide a syntactic sugar, is “partial” critical in any other ways? The python api for “partial” relates the usage to functions, so the usage of partial against the “optim.Adam” class is even more beguiling to me. Wonder how that works.

Appreciate any feedback… I’m just a plain old Java guy who’s having a heck-of-a-butt-slapping in the pythonic way of doing things :slight_smile:

1 Like

partial is used when you don’t have ALL of the arguments for a function (or class constructor), but have a partial list of them and want to specify it before kicking the can to later down the road to fill the remaining arguments. That way you don’t have to Keep passing all the arguments around just to invoke the function later. IMO - It results in cleaner code and less API dependency.


There is a related concept called currying which is quite neat as well. In case you’d like to read more about it there is this post that rather nicely speaks to the differences.

Fun fact: currying is named after Haskell Curry!