Python functools.partial Tutorial: A modern approach to creating partial functions
I don’t know if you have ever had such an experience: a function has many parameters, but several parameters always have fixed values every time it is called. Repeated writing is troublesome and makes the code look bloated. For example, useintWhen converting a binary string, add it every timebase=2, or useroundAlways write when retaining two decimal placesndigits=2. At this time, the Python standard libraryfunctools.partialcan help us solve this problem.
What is a partial function?
In functional programming, the core idea of partial functions is partial application: generating a new function by "freezing" some parameters of the original function in advance (which can be positional parameters or keyword parameters). When calling the new function later, you only need to pass in the remaining parameters.
💡 Don't get confused: The "partial function" here is completely different from the "partial function" in mathematics. In mathematics, we talk about functions whose domain is not the complete set; and in programming, partial functions are simply "functions that fill in some parameters first."
Why do we need partial functions?
Partial functions can make our code cleaner and easier to maintain, which is reflected in the following aspects:
- Simplify repeated calls: When certain parameters remain unchanged in most scenarios, there is no need to write them again every time they are called;
- Improve readability: Give the newly generated partial function a semantically clear name so that the intent of the code is clear at a glance;
- Reduce duplicate code: There is no need to encapsulate a new function specifically for a few fixed parameters and avoid boilerplate code.
Basic usage
Let’s first look at the two most commonly used introductory examples to get a feel for it.functools.partialconvenience.
Example 1: Convert fixed base string to integer
int()You can convert a string into an integer, the default is decimal. If you want to parse in binary or octal, you must pass it every timebaseparameter. usepartialJust put itbasePin it in advance:
Example 2: Fixed precision rounding
round()FunctionalndigitsThe parameter controls how many decimal places are retained. If we only need to keep two digits most of the time, we can also usepartialEncapsulate it:
Advanced usage
In addition to simply fixed keyword parameters,partialIt can also handle more flexible parameter combinations.
1. Fixed position parameters
partialParameters can be passed in directly by position, and these parameters will be placed at the front of all parameters when the new function is called:
2. Fixed positional parameters and keyword parameters at the same time
For custom functions, we have more flexibility in mixing fixed positional and keyword arguments:
3. How to "update" the parameters of a partial function?
partialThe original function is saved in the object (funcproperties) and fixed parameters (argsandkeywordsproperty). However, it is usually not recommended to modify these properties directly. A safer approach is to regenerate a new partial function based on the original function:
Improvements in modern Python
Before Python 3.10,partialObject__name__and__doc__Properties are not friendly, you may see when debugging'partial'Such a name. Starting from 3.10,partialThese two attributes of the original function will be inherited by default, making debugging and documentation clearer:
Comparison with lambda expressions
In some scenarioslambdaSimilar functions can also be achieved, butpartialUsually more advantageous:
Both have the same effect, butpartialThere are several benefits:
- When there are many fixed parameters,
partialMore concise, no need to write repeated parameter lists; partialWill retain the meta-information of the original function (such as 3.10+'s__name__、__doc__),andlambdaWon't;- For those familiar with functional programming,
partialThe intention is clearer.
Of course, if in addition to parameter fixation, you need to do some additional simple logic processing, you still have to uselambda。
Best Practices
in order to letpartialTo use it better, here are a few suggestions:
- Name should be semantic: Give the partial function a name that expresses its purpose, such as
int2、round2, instead of just callingp1、p2; - Prioritize using keyword parameters to fix later parameters: If the fixed parameter is not the first positional parameter, using keyword parameters is safer and can avoid bugs caused by parameter order changes;
- Don’t overuse: Only use fixed parameters when it can really simplify the code. Using them for the sake of use will increase the reading burden;
- Added type hints (Python 3.10+): It can make the type of partial functions clearer and the editor prompts more accurate:
Actual application scenarios
partialIt is used a lot in daily development, such as:
- GUI / Asynchronous callback: The button click callback needs to pass in the button ID, use
partialFix the ID first so you don’t have to write it every timelambdaThe trouble of passing parameters; - Data processing pipeline: When cleaning data, most parameters of a certain cleaning function are fixed. Use
partialEncapsulated into dedicated cleaning steps; - API packaging: Simplify the common calling methods of third-party complex APIs and fill in common configurations in advance.
Summarize
functools.partialIt is a small but very practical tool. Its core is to "fill in some parameters first and talk about the rest later." Reasonable use of it can help us reduce repeated code and make the code more concise and readable.
Next time you encounter a scenario where you need to pass the same parameters repeatedly, you might as well try it to make the code cleaner~

