Skip to main content

A lightweight extension of functools.partial

Project description

impartial

build PyPI PyPI - Python Version Licence

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)
9

Features:

  • the with_<keyword>(value) methods can be arbitrarily chained
  • impartial functions are immutable: any "modification" of arguments returns a new impartial function
  • very lightweight (~50 LOC and no dependencies)
  • fully compatible with functools.partial (impartial is a subclass of functools.partial)
  • can be used as a decorator

To install this package, run:

pip install impartial

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

impartial-0.2.0.tar.gz (7.4 kB view hashes)

Uploaded Source

Built Distribution

impartial-0.2.0-py3-none-any.whl (11.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page