partial

class iteration_utilities.partial(func, *args, **kwargs)

Like functools.partial() but supporting placeholders.

New in version 0.4.0.

Parameters:

func : callable

The function to partially wrap.

args : any type

The positional arguments for func.

Note

Using partial._ as one or multiple positional arguments will be interpreted as placeholder that need to be filled when the partial instance is called.

kwargs : any type

The keyword arguments for func.

Returns:

partial : callable

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 args they can’t be used for the keywords.

Examples

The iteration_utilities.partial can be used as slightly slower drop-in replacement for functools.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). The partial._ attribute or the Placeholder or instances of PlaceholderType() can be used as placeholders for the positional arguments.

For example most iterators in iteration_utilities take 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) using func. The all_args are the args combined with the additional_args. Likewise the all_kwargs is a mapping created from the keywords with the additional_kwargs.

_

(Placeholder) Allows easy access to a placeholder without having to import Placeholder.

__dict__

(dict) instances have a normal __dict__ member and support instance attributes.

args

(tuple) arguments for future partial calls (readonly).

func

(callable) Function object to use in future partial calls (readonly).

keywords

(dict) keyword arguments for future partial calls (readonly).

num_placeholders

(int) Number of placeholders in the args (readonly).