snakeoil.currying module¶
Function currying, generating a functor with a set of args/defaults pre bound.
pre_curry()
and post_curry()
return “normal” python functions
The difference between pre_curry()
and functools.partial()
is this
>>> from functools import partial
>>> from snakeoil.currying import pre_curry
>>> def func(arg=None, self=None):
... return arg, self
>>> curry = pre_curry(func, True)
>>> part = partial(func, True)
>>> class Test:
... curry = pre_curry(func, True)
... part = partial(func, True)
... def __repr__(self):
... return '<Test object>'
>>> curry()
(True, None)
>>> Test().curry()
(True, <Test object>)
>>> part()
(True, None)
>>> Test().part()
(True, None)
If your curried function is not used as a class attribute the results should be
identical. Because functools.partial()
has an implementation in C
while pre_curry()
is python you should use functools.partial()
if possible.
- snakeoil.currying.post_curry(func, *args, **kwargs)[source]¶
passed in args are appended to any further args supplied
- snakeoil.currying.pre_curry(func, *args, **kwargs)[source]¶
passed in args are prefixed, with further args appended
Unlike partial, this is usable as an instancemethod.
- snakeoil.currying.pretty_docs(wrapped, extradocs=None, name=None)[source]¶
Modify wrapped, so that it ‘looks’ like what it’s wrapping.
This is primarily useful for introspection reasons- doc generators, direct user interaction with an object in the interpretter, etc.
- Parameters:
wrapped – functor to modify
extradocs –
__doc__
override for wrapped; else it pulls from wrapped’s target functorname –
__name__
override for wrapped; else it pulls from wrapped’s target functor for the name.