X-partial¶
More powerful partial application with the X object
-
pipetools.
xpartial
(func, *xargs, **xkwargs)¶ Like
functools.partial()
, but can take anXObject
placeholder that will be replaced with the first positional argument when the partially applied function is called.Useful when the function’s positional arguments’ order doesn’t fit your situation, e.g.:
>>> reverse_range = xpartial(range, X, 0, -1) >>> reverse_range(5) [5, 4, 3, 2, 1]
It can also be used to transform the positional argument to a keyword argument, which can come in handy inside a pipe:
xpartial(objects.get, id=X)
Also the XObjects are evaluated, which can be used for some sort of destructuring of the argument:
xpartial(somefunc, name=X.name, number=X.contacts['number'])
Lastly, unlike
functools.partial()
, this creates a regular function which will bind to classes (like thecurry
function fromdjango.utils.functional
).