functools.partial
Create a new function with some of the arguments set in advance.
Python 3.13
functools.partial(func, /, *args, **keywords)Parameters
- funcCallable
- The original function.
- *argsAny
- Positional arguments that go first.
- **keywordsAny
- Keyword arguments; they can be overridden in a call.
Return value
A partial object that is called like a regular function.
Examples
A function with a ready greeting
Python 3.13
from functools import partial
def greet(greeting, name):
return f'{greeting}, {name}!'
hello = partial(greet, 'Hello')
print(hello('Anna'))Hello, Anna!
int with base 2
Python 3.13
from functools import partial
from_binary = partial(int, base=2)
print(from_binary('1010'))10
Details
Arguments passed in a call are added after the preset ones. Keyword values can be replaced in a call: from_binary("12", base=10) gives 12. The original function and arguments are stored in the func, args and keywords attributes.
