Patternite Logo

Create a function with a subset of arguments in Python


By using the built-in `partial` function, we can "fill in" function arguments, to create a new function with a smaller signature


Partial functions in Python are especially useful when a function must be passed to another function or method. In these circumstances, we may want to set arguments in the passed function, such as the keyword argument round_result on line #9. Using the built-in partial function, we can set these arguments and create a new function with a smaller signature.

In this example, score_function_with_rounding is a function with only a single argument, data. Since the Evaluator's score method does not care about any other arguments (such as round_result), we can safely use the partial function instead of score_function.

A more in-depth overview of the partial function can be found in the official Python docs.

Profile picture for duncster

| edited

Patternite © 2021

Patternite Logo