Welcome to partial-apply’s documentation!¶
Partial application of functions and method names, supporting placeholder values for positional arguments.
Unlike functools.partial()
, placeholder values are supported so that
positional arguments for partial application do not need to be supplied solely
from left to right. Keyword arguments are handled equivalently to
functools.partial()
. It is also possible to “partially apply” a method
name, producing a function which looks up the method to call on the object
supplied as its first argument.
-
partial_apply.
Empty
= Empty¶ A placeholder for arguments which will be supplied later.
-
class
partial_apply.
PartialFn
(func, *args, **kwargs)[source]¶ A function partially applied to positional and keyword arguments.
Keyword arguments are handled equivalently to
functools.partial()
, but arbitrary sets of positional arguments can be partially applied.Examples: Skipping partial application of positional arguments using the
Empty
placeholder:Create a partially applied function from
fn
where the third argument is'thing'
.>>> def fn(*args): >>> return args
>>> p = PartialFn(fn, Empty, Empty, 'thing') >>> p PartialFn(<function fn at 0x11b29a598>, Empty, Empty, 'thing')
With two placeholders, you must call
p
with at least two positional arguments. They fill in the placeholders from left to right.>>> p(1, 2) (1, 2, 'thing')
Subsequent positional arguments to
p
are applied after the third parameter.>>> p(1, 2, 3) (1, 2, 'thing', 3)
Variables: - func (callable) – The wrapped function.
- args (tuple) – Positional arguments for the wrapped function.
- kwargs (dict[str, any]) – Keyword arguments for the wrapped function.
-
__init__
(func, *args, **kwargs)[source]¶ Creates a new partially applied function.
Parameters: - func (callable) – The function to wrap.
- args – Positional arguments for the wrapped function.
- kwargs – Keyword arguments for the wrapped function.
Raises: TypeError – If the first argument is not callable.
-
__call__
(*args, **kwargs)[source]¶ Calls the partially applied function.
Parameters: - args – Additional positional arguments for the wrapped function.
- kwargs – Additional keyword arguments for the wrapped function.
Returns: The return value of the wrapped function.
Return type: any
Raises: TypeError – If less positional arguments are supplied than placeholders.
-
class
partial_apply.
PartialMethod
(name, *args, **kwargs)[source]¶ A method name partially applied to positional and keyword arguments.
Keyword arguments are handled equivalently to
functools.partial()
, but arbitrary sets of positional arguments can be partially applied (seePartialFn
for an example). When an instance ofPartialMethod
is called, the object to look up the method on is supplied as the first argument.Examples: Setting up some example classes.
>>> class SomeClass: >>> def method(self, *args): >>> return args
>>> some_instance = SomeClass()
>>> class OtherClass: >>> def method(self, *args): >>> return sum(args)
>>> other_instance = OtherClass()
Create a partially applied function where the method name is
method
and the second positional argument is 1.>>> p = PartialMethod('method', Empty, 1) >>> p PartialMethod('method', Empty, 1)
The partially applied function takes the object to look up the method on as the first argument. Subsequent positional arguments are handled as in
PartialFn
. Apply the function to different objects of different types (they must all have a method namedmethod
).>>> p(some_instance, 2, 3) (2, 1, 3)
>>> p(other_instance, 2, 3) 6
Variables: - name (str) – The method name.
- args (tuple) – Positional arguments for the method.
- kwargs (dict[str, any]) – Keyword arguments for the method.
-
__init__
(name, *args, **kwargs)[source]¶ Creates a new partially applied method name.
Parameters: - name (str) – The method name.
- args – Positional arguments for the method.
- kwargs – Keyword arguments for the method.
Raises: TypeError – If the first argument is not a string.
-
__call__
(obj, *args, **kwargs)[source]¶ Looks up and calls the method.
Parameters: - obj (any) – The object to look up the method on.
- args – Additional positional arguments for the method.
- kwargs – Additional keyword arguments for the method.
Returns: The return value of the method.
Return type: any
Raises: TypeError – If less positional arguments are supplied than placeholders.