impartial is a lightweight extension of functools.partial that allows modifying positional and keyword arguments in a functional style.
The main idea is that any function wrapped with impartial gets a method with_<keyword>(value) for every keyword argument of that function.
Each with_<keyword>(value) method returns a new impartial function with that keyword being modified.
>>> from impartial import impartial
>>> @impartial
... def power(x, exponent):
... return x ** exponent
...
>>> power
impartial(<function power at 0x10d54e790>)
>>> square = power.with_exponent(2) # behaves like functools.partial(square, exponent=2)
>>> square
impartial(<function power at 0x10d54e790>, exponent=2)
>>> square(3)
9Features:
- the
with_<keyword>(value)methods can be arbitrarily chained impartialfunctions are immutable: any "modification" of arguments returns a newimpartialfunction- very lightweight (~50 LOC and no dependencies)
- fully compatible with functools.partial (
impartialis a subclass offunctools.partial) - can be used as a decorator
To install this package, run:
pip install impartial