partial¶
- class iteration_utilities.partial(func, *args, **kwargs)¶
Like
functools.partial()but supporting placeholders.Added in version 0.4.0.
- Parameters:
- funccallable
The function to partially wrap.
- argsany type
The positional arguments for func.
- kwargsany type
The keyword arguments for func.
- Returns:
- partialcallable
The func where the given positional arguments are fixed (or represented as placeholders) and with optional keyword arguments.
Notes
While placeholders can be used for the
argsthey can’t be used for thekeywords.Examples
The
iteration_utilities.partialcan be used as slightly slower drop-in replacement forfunctools.partial(). However it offers the possibility to pass in placeholders as positional arguments. This can be especially useful if a function does not allow keyword arguments:>>> from iteration_utilities import partial >>> isint = partial(isinstance, partial._, int) >>> isint(10) True >>> isint(11.11) False
In this case the isint function is equivalent but faster than
lambda x: isinstance(x, int). Thepartial._attribute or thePlaceholdercan be used as placeholders for the positional arguments.For example most iterators in
iteration_utilitiestake the iterable as the first argument so other arguments can be easily added:>>> from iteration_utilities import accumulate, Placeholder >>> from operator import mul >>> cumprod = partial(accumulate, Placeholder, mul) >>> list(cumprod([1,2,3,4,5])) [1, 2, 6, 24, 120]
- __call__(*additional_args, **additional_kwargs)¶
Returns
func(*all_args, **all_kwargs)usingfunc. Theall_argsare theargscombined with theadditional_args. Likewise theall_kwargsis a mapping created from thekeywordswith theadditional_kwargs.
- __sizeof__()¶
Returns size of the instance in memory, in bytes.
- _¶
(
Placeholder) Allows easy access to a placeholder without having to importPlaceholder.
- func¶
(callable) Function object to use in future partial calls (readonly).