partial
- class utilitools.partial(function, /, *args, **kwargs)[source]
- An extended version (handling positional arguments) that inherits from functools.partial.
- static __new__(cls, function, /, *args, **kwargs)[source]
- Creates an object with a simplified signature by “freezing” some portion of a function’s positional and keyword arguments.
- Parameters
function (collections.Callable) –
Callable to which the partial object will be applied.args (Any) –
Variadic positional arguments will be passed directly to the function during initialization.kwargs (Any) –
Variadic keywords arguments will be passed directly to the function during initialization.
- Returns
A partial object with a simplified signature.
- Return type
- Raises
TypeError –
If function is not callable.
- __call__(*args, **kwargs)[source]
- An extension that accepts Ellipsis as a placeholder for positional arguments.
- Parameters
args (Any) –
Variadic positional arguments will be passed directly to the function after initialization.kwargs (Any) –
Variadic keywords arguments will be passed directly to the function after initialization.
- Returns
The output of the function.
- Return type
Any
- Raises
BaseException –
If args does not match to function.
If kwargs does not match to function.
Examples
from utilitools import partial
def variadic_positional(*args):
return args
def positional_only(a, b, c, d=4, e=5, f=6, /):
return a, b, c, d, e, f
def positional_or_keyword(a, b, c, d=4, e=5, f=6):
return a, b, c, d, e, f
Partial function with variadic positional arguments:
>>> partial_func = partial(variadic_positional, ..., ..., 3, 4)
>>> partial_func(1, 2, 5, 6)
(1, 2, 3, 4, 5, 6)
Partial function with positional-only arguments:
>>> partial_func = partial(positional_only, ..., ..., 3, 4)
>>> partial_func(1, 2, 5)
(1, 2, 3, 4, 5, 6)
Partial function with positional or keyword arguments:
>>> partial_func = partial(positional_or_keyword, ..., 2, ..., ..., 5)
>>> partial_func(1, 3, 4)
(1, 2, 3, 4, 5, 6)
>>> partial_func = partial(positional_or_keyword, ..., 2, ..., e=5)
>>> partial_func(1, 3, d=4)
(1, 2, 3, 4, 5, 6)